Usually, while storing the data in a variable we store a single value, for example, x = 5. With the help of Lists in Python, we can store multiple values in a single variable. Lists are a sequence of elements. These elements are also referred to as itemsof the list and can be of various data types.
Creating a List
To create a list in Python, we can place all the elements inside the square brackets [], where each element is separated by commas. We can have n number of items in the list and the items can be of any data type.
##Creating a List
l = [100,101,102,103]
print(l)
Output:
[100, 101, 102, 103]
#List containing various datatypes
l = ['red', 1, 2, 3.14,'green']
print(l)
Output:
['red', 1, 2, 3.14, 'green']
In our first example here, we have four integer values in the list whereas in the second example we observe that the list items are of various data types, i.e., string, integer, and float in this case.
In Python, we can also create an empty list.
#Creating an empty list
l = []
l = list()
Output:
[]
A list can also contain another list nested within it.
#Nested List
l = [['red', 'green'],1, 2, 3.14]
print(l)
Output:
[['red', 'green'], 1, 2, 3.14]
Accessing Elements of List
Lists in Python are ordered and hence we can access the elements of the list with the help of indexing and slicing.
Since the lists are ordered the indexing in list start from 0.
#List indexing
l = ['red', 1, 2, 3.14,'green']
#For first element
print(l[0])
#For third element
print(l[2])
Output:
red
2
green
We can access the elements of the list in Python by using negative indexing also. The negative indexing starts from the last element with the index value -1.
#Negative Indexing
l = ['red', 1, 2, 3.14,'green']
#For last element
print(l[-1])
#For third last element
print(l[-3])
Output:
green
2
To retrieve more than one element of the list we perform slicing on the list using the slicing operator ‘:’ (colon). In slicing, we specify a range of indexes by specifying the start and the end.
#Slicing a list
l = [1000,1001,1002,1003,1004]
print(l)
#Returns all the elements from 2nd element till 4th element
print(l[1:4])
Output:
[1001, 1002, 1003]
Here we can notice that in slicing operator [1:4] it includes the element at index 1 but goes up to index 4 and hence does not include the element at index position 4.
#Returns all the elements from the third element
print(l[3:])
Output:
[1003, 1004]
#Returns all the elements upto index 3
print(l[:3])
Output:
[1000, 1001, 1002]
Length of List
We can find out about the total number of elements in the list by using the len() function in Python.
#Length of a list
l = [1000,1001,1002,1003,1004]
print(len(l))
Output:
5
Therefore, here we can notice that the len() function tells us that there are in all 5 elements in the list l.
Changing Elements of List
Lists in Python are mutable, that means we can change the elements of the list.
#Changing the elements of list
fruits = ['apple','bear','cherry']
print(fruits)
fruits[1] = 'banana' #Changing bear to banana
print(fruits)
Output:
['apple', 'bear', 'cherry']
['apple', 'banana', 'cherry']
Therefore, these are the following ways you can work around with lists in Python.
This article is written and published by Jinal Shah.