Lists in Python
As we are going through various data type available in the python. In this article, we are going to explore more about the most used data structure list/sequence. List in python is one of the most basic data structure. Each element of the list is assigned a number, which is called index of the element. The first element’s index will be zero, then all subsequent element have index in incremental order. List are immutable in Python, which means python will not create a new list if we modify an element of the list. List is created by storing a sequence of different types of values separated by commas.
Python list is enclosed between
my_list = [1,2,3,4,7] my_list = [1,2,”String1”,”String2”]
Similar to string indices, list indices start at 0, and lists can be sliced, concatenated and so on.
Accessing Values in Lists:
To access values in lists, use the square brackets for slicing along with the index or indices to obtain value available at that index as shown in below example:
my_list = [1, 2, 3, 4, 7] my_mix_list = [1, 2,"String1", "String2"] print("my_list: "+ str(my_list)) print(my_mix_list[2:])
This code will produce below output:
Output: my_list: 3
Append or update lists:
Let's assume we have a list called my_list and it is defined as below. Now we will see various operation over it.
my_list = [1, 2, 3, 4, 7]
Add New element
my_list.append(“Append This!!”) print(my_list)
[1, 2, 3, 4, 7, “Append This!!”]
To update Element in the list we can directly use list index where we want to update the value as below:
my_list = 5 print(my_list)
[1, 2, 3, 4, 5, “Append This!!”]
To delete the element at any index we can use
del my_list print(my_list)
[1, 2, 3, 4, 7]
There are many built-in functio
Methods with Description
Returns count of occurrence of the object in
Sorts objects of
Appends the contents of
Return the index of the first occurrence of the object
Appends object obj to list
Removes and returns last object or obj from list
Inserts object obj into
Removes object obj from list
This will reverse the objects in the list
Function with Description
Compares elements of both lists.
Return the minimum from the list
Converts given tuple to list
Returns the maximum from the list
That's it for the list. If you play little bit more with the methods above, you will get to understand what each of the methods are doing and where which method/function can be used.
That's all for this topic. If you guys have any suggestions or queries, feel free to drop a comment. We would be happy to add that in our post. You can also contribute your articles by creating contributor account here.
Happy Learning 🙂
If you like the content on CodePumpkin and if you wish to do something for the community and the planet Earth, you can donate to our campaign for planting more trees at CodePumpkin Cauvery Calling Campaign.
We may not get time to plant a tree, but we can definitely donate ₹42 per Tree.
About the Author
Comments and QueriesIf you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
<pre><code class="java"> String foo = "bar"; </code></pre>For more information on supported HTML tags in disqus comment, click here.