Today we going to learn about Python List. Earlier we learnt about Python Numbers which can be found here.
Python List
List is a versatile datatype available in Python. Basically a python list is comma-separated values which are called items. List in python is written within square brackets. Interestingly it’s not necessary for items in a list to be of same types. For example;
#an empty list
empty_list=[]
#a list of strings
str_list=['this', 'is', 'a', 'list']
# a list of integers
int_list=[1,2,3,4,5]
#a list of mixed type of items
mixed_list=['this', 1, 'is', 2, 'a', 3, 'mixed',4, 'list',5]
# to print the lists
print(empty_list)
print(str_list)
print(int_list)
print(mixed_list)
The above code will produce the following output.
Accessing Items in Python List
Each item of a list is assigned a number – it’s position or index. The first index is zero, the second index is one, and so forth.
To access items in a list, we can use these index number within a square bracket. For example;
#a list of strings
str_list=['this', 'is', 'a', 'list']
#to access first item
print(str_list[0])
#to access second item
print(str_list[1])
#to access 4th element
print(str_list[3])
The above code will produce output like below.
Surprising fact is index can be negative. It means to read not from the left rather from the right of the list.
#a list of strings
str_list=['this', 'is', 'a', 'list']
#third item from left
print(str_list[2])
#third item from right
print(str_list[-3])
The output of the above code will be like below-
Update A List Item
We can update one or more item of a list simply through the index of that item.
#a list of strings
str_list=['this', 'is', 'a', 'list']
print("before updating the list: ")
print(str_list)
str_list[3]='updated list'
print("after updating the list: ")
print(str_list)
The output will be like below.
Deleting an item in a list
To delete an item in a list, there are several methods. Look at the following example to explore it further.
#an empty list
empty_list=[]
#a list of strings
str_list=['this', 'is', 'a', 'list']
#to remove a specific element, like 'is'
str_list.remove('is')
print(str_list)
#to remove an item of a specific index like 2
del str_list[2]
print(str_list)
#there are yet another way to remove an item of a specific index
str_list.pop(0)
print(str_list)
The above code will produce output like below.
Some built-in python list functions
There are some built-in functions to manipulate list in python. Let’s look at the following example for understanding.
#an empty list
empty_list=[]
#a list of strings
str_list=['this', 'is', 'a', 'list']
# add an element to the end of the list
str_list.append('appended')
print(str_list)
#insert an item at the defined index
str_list.insert(3,'inserted')
print(str_list)
#to get the index of the first matched item
print(str_list.index('a'))
#to count number of a specific element in a list
print(str_list.count('is'))
#to reverse the order of a list
str_list.reverse()
print(str_list)
#to sort the list in ascending order
str_list.sort()
print(str_list)
The output of the above code will be as follows.
So this is all about python lists for now. Make sure to run every piece of code on you own. Feel free to leave a comment if you have any doubt.
#happy_coding 🙂
Reference: https://docs.python.org/3.6/tutorial/datastructures.html