Lists in Python

Beginners Code Computer Skills Python

Lists in Python:

From this section onwards we would proceed to little advanced uses of python language. We would be studying the various data structure formats associated with Python. In general, we have six different types of data structure in python. But the most common ones and the most frequently used ones include lists and tuples.

The important concept involved here is storing of data in a sequence and manipulating the data according to our needs. It means we can store as much data as we want in any form of python datatype. Thus we can perform a wide range of operations.

Thus List stands out as an important and most powerful data type of python. The unique feature is that we can store any amount of data in a list and it is not necessary for the data to be of the same type.

Thus in simple words, we can say, a list is a regular array that can store data of multiple data types.

Reading values of a List:

Reading values from a list are the same as that of accessing values from an array.

The general syntax is:

sample_list = ["mario", "mushrooms", 'a', 1000, 20.35]
for i in sample_list:

Basic operations on Lists:

Lists can be used to perform all the basic operations as we do with two different variables in python. Some common operations include finding the length of the list, concatenation of two lists, Searching elements, Repeating in a particular sequence and Iteration. The operations are extremely easy and mostly resemble what we know about arrays. Implement the following code to have a clear overview once again.

sample_list_1 = ["mario", "mushrooms", 'a', 1000, 20.35]

for i in sample_list_1:
# finding the length of a list using the len keyword
length = len(sample_list_1)

sample_list_2 = ["Tony", "Hulk", 'g', 3000, 14.6]

# Concatenate two lists simply by using the + operator
concatenated_list = sample_list_1 + sample_list_2

# perform a Search similar to that in an array
for i in sample_list_2:
    if i == "Tony":
        print("Tony is alive")
        print("Tony is Dead")

# Repeat a list using * operator
repeat_list = sample_list_2 *4

Deletion and Updation of List:

To delete elements from a list we use the del keyword if you know the location of the item to be removed or else use the inbuilt remove function to remove an element from the list.

Similarly, updating elements in a list is also fairly simple. We just need to mention the location which we want to update by removing old values and simply assign the new value we wish using the assignment operator.

We can also add new individual values by using the append function. It goes on extending the list space as we go on adding the new values.

Thus, we see how versatile a list can be when it comes to comparison with an array in terms of performing the basic operations. The following code will give you a clear overview of Deletion and Updation in a list:

When we know the position to delete we can write it as:

sample_list = ["Tony", "Hulk", 'g', 3000, 14.56]

for i in sample_list:

del sample_list[2]


When we know the item to remove, but not the position, we can write it as:

sample_list = ["Tony", "Hulk", 'g', 3000, 14.56]

for i in sample_list:



Similarly while updating the list, if we know the position, then we can write it as:

sample_list = ["Tony", "Hulk", 'g', 3000, 14.56]

for i in sample_list:

sample_list[2] = "groot"


But if we want to add an entirely new element, we use the append function as:

sample_list = ["Tony", "Hulk", 'g', 3000, 14.56]

for i in sample_list:



Note that, adding multiple elements to an existing list is same a creating a list of new elements as concatenating to the older list.

These were some basic yet highly handy features of a list. In the next section, we will be learning about another type of data structure called as Tuples.