Lists in Python
Topics covered in the last tutorial, Basics of Strings and Arithmetic Calculations. In this tutorial we will be discussing about the lists in python.
Lists:
Lists are a type of data structure in python, which can hold different forms of data types( int, float, char…. ) unlike in other programming languages. Each element in a list is known as “item”. Each item is separated by a comma (,) and if the item is a character it should be enclosed within apostrophe and integers can withstand alone. All the items should be enclosed within square brackets[]. Lists are mutable which means, the programmer can add, remove, interchange the values in a list.
E.g: our_list = [1,’a’,2,’b’]
This is list of 3 items in it.
my_list = [1,’a’,2,’b’] #declaring a list
my_list #accessing a list
[1,’a’,2,’b’] #output
print(my_list) #accessing list in another way
[1,’a’,2,’b’] #output
#Indexing and slicing of list and their outputs
my_list[0] >> 1
my_list[-3] >>’a’
my_list[:] >>[1,’a’,2,’b’]
my_list[-3:] >>[‘a’,2,’b’]
Operations on Lists:
To perform changes in a list, We have to type the list name and put a dot (.) after it and should hit TAB button.
append(): This function takes an item as an argument as adds that in the list at last position. For example my_list.append(7) then my_list = [1, ’a’, 2, ’b’, 7 ] , this would be the final list.
clear(): This function would clear all the elements of the list and a null list will be created on the same name. For example: my_list.clear()
then my_list = [ ] , this would be the final list.
copy(): This function creates a duplicate copy of the list.
count(): This function takes an argument element that is present in the list and gives the number of times that particular element is repeated in that list. For example my_list.count(1) #(my_list = [1, ’a’, 2, ’b’, 7 ])
then the output would be 1.
extend(): This function takes the argument as the name of other list and the other list items can add to present list.
For example: other_list = [4, ‘gd’ ,6 ]
my_list.extend(other_list)
then my_list = [1, ’a’, 2, ’b’, 7, 4, ‘gd’ ,6 ] , this would be final list.
index(): This function takes the argument as one of the elements of the list and gives its index value. If the given element is recurring many times in the list, then the first index value will be as output. For example: my_list = [1, ’a’, 2, ’b’, 7, 4, ‘gd’ ,6 ]
my_list.index(7)
4 #this will be output
insert(): This function takes two arguments, the first argument is the position where you wanted to insert the element, the second argument will be the element that we wanted to insert in the list.
pop(): This function is also useful in many other programming languages which means remove. This function removes the last element of the list. This function also works in other way, give any index number as function argument and this function would remove the particular element that belongs to that index value.
remove(): This function removes the element which is given as argument.
reverse(): This function reverses all the elements of the list.
Sort(): This function only works when all the elements of the list are of same data type. This function sorts the elements either from ascending to descending or in reverse order according to user guidance.
Nested list:
A nested list is a list that contains a list as its element. E.g: my_list = [1 , 4, 5, [‘a’, ‘b’ ,’c’ ]] Accessing elements in a nested list:
To access ‘b’ from the above example
my_list[3][1]
If you are new to Python. You can check out the previous article to cover the basics: Click Here. Things we shall cover in next tutorial:
- Dictionary
- Tuples
This is all about this tutorial lists in Python. For more information visit the Data Science section.