List is a data type that is used a lot in Python. They are basically containers that can have multiple kinds of objects in it – strings, characters, integers; even a list itself!
Creating a list and printing it:
So, how do we create a list? Well, a list is depicted by , and the objects in it are separated by a ‘,’.
Let us create a list.
Here, we have created a list whose name is my_list. If we run our program now, our list will get printed-
Accessing elements of a list and manipulating them Now, let us try to access individual elements of a list. We can do this by using the index number of
that element, which denotes the position of the element in the list. Note that the index begins from 0 and not 1. So, if we want to access the 2nd element of the above list, and print it, we will do a follows-
And we get this as the output-
Note: We have taken the index as one, as the index begins from 0. So, the second element will have the index 1 and not 2. An important property of lists is that they are mutable- We can change them. This is as easy as reassigning a variable. We pick the element that we want to change using its index, and then simply reassign it.
my_list='Hello World' print(my_list)
And when we run this, we get the following output-
[1, 3.4, 'Hello World', 'z']
We can see that the element at the third position (index=2) gets reassigned. This property of lists is called mutability- We can change the elements of a list after creating it.
Length of a list:
Now, let’s see how we can find out the length of a list. The length of a list is defined as the number of elements present in it. We can find out the length of a list using the inbuilt function len(). The argument (the stuff that goes inside the brackets) will be the name of the list. So, we use it as follows-
The output will be-
which is the length of our list.
Finding an element in a list:
Now, how about we do something interesting? Say, we want to find a particular object in a list, a number, a float value or a string. How can we find it? Well, there are many ways to solve this problem, and the easiest one involves running the list through a loop. But we will see this method later on. Right now, we are going to use a technique unique to Python.
We use the ‘in’ operator. Suppose we want to check the presence of two items in our list: ‘Hello World’ and the number 7. We can see that while the first item is present, the second one is not.
Here is how we check the presence of these two words using the in operator in Python: –
Check_presence='Hello World' in my_list print(Check_presence) Check_presence=7 in my_list print(Check_presence)
The output is –
The ‘in’ operator checks our list and tries to find the item we are looking for. If it is present, it returns the Boolean value True, and if it is not present, it returns False. Hence, the output is True and False.
Slicing a list:
Sometimes, we want to take a particular part of a list for computation, and not the entire list. This can happen in many cases, Eg: – You have a list which is sorted in increasing order, and contains the numbers from -10 to 10. You know the index of ‘0’, and want to take only the positive numbers from
the list. How will you take this portion from this list? Don’t worry! Python has a nice trick up its sleeve to solve this problem.
Here’s the basic principle that we have to follow to take a ‘slice’:
Let us take a new list-
Suppose we want to take the part [2,3,4] from the list and print it. Here’s how we define this slice-
And this is the output that we will get-
Here’s the basic format that we have to follow to take a slice-
start → Numerical value of the index from where the slice starts. In the above example, the value of start will be the value of the index of element ‘2’, which is 1.
stop → Numerical value of the index that we will go up to, but not include in our slice. In the above example, the value of stop will be 4, as we want the elements from 1 to 3. Note that stop will not be 3, as we have to include the value at that index as well. So, stop denotes the value of the index which
is one more than the index of the last element of the slice.
step → The size of the jump that we take while moving from one index value to the other. In the above example, the step size is 1, as we want to take all the elements starting from ‘start’ and ending before ‘stop’ indices.
There is much more to list slicing, and we will have look upon these things at a later stage. Until then, have fun coding!