Code Pumpkin

Lists in Python

October 27, 2018
Posted by Dipen Adroja

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 square([]) brackets and elements are stored in the index basis with starting index 0.

Defining Lists:

List syntax:

<list_name>=[value1,value2,…,valuen];  

Example:


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[2]: "+ str(my_list[2]))
print(my_mix_list[2:])

This code will produce below output:

Output:   my_list[2]: 3

              [‘String1’, ‘String2’]

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 in the list:

To add new element to list we can use append method of list as below:


my_list.append(“Append This!!”)
print(my_list)

Output:

[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[4] = 5
print(my_list)

Output:

[1, 2, 3, 4, 5, “Append This!!”]

To delete the element at any index we can use del function as below:


del my_list[5]
print(my_list)

Output:

[1, 2, 3, 4, 7]

There are many built-in functions and methods that come with List below is the list for the same.

Methods with Description

list.count(obj)

Returns count of occurrence of the object in list

list.sort([sortme])

Sorts objects of list, use compare func if given

list.extend(let)

Appends the contents of lst to list

list.index(obj)

Return the index of the first occurrence of the object

list.append(obj)

Appends object obj to list

list.pop(obj=list[-1])

Removes and returns last object or obj from list

list.insert(index, obj)

Inserts object obj into list at given offset index

list.remove(obj)

Removes object obj from list

list.reverse()

This will reverse the objects in the list

Function with Description

cmp(list1, list2)

Compares elements of both lists.

min(list)

Return the minimum from the list

len(list)

Returns to length of the list

list(seq)

Converts given tuple to list

max(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


Coder, Blogger, Wanderer, Philosopher, Curious pumpkin



Tags:


Comments and Queries

If 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.
Total Posts : 124
follow us in feedly

Like Us On Facebook