Register yourself to get hired soon:- Register Now
Python lists are mutable and collections of values. which is order and duplicate values will be allowed in lists. Python lists are the data structure. That is capable of holding a different type of data. A list can be composed by storing a sequence of different types of values separated by commas.
A Python list is enclosed between square [] brackets and elements are stored in the index bases with storing index as o or -1.
Not- Important points. you should keep in mind that without practice no one can be a programmer you have to be practice a lot. To understand the code. so in this article we will learn every operation with interactive example.
There will be two types of output representation. first is in the form of output and second in the form of a comment using '#' special character.
For Example.
x=[5,10,15,20,25,30] y=[5,'x',10.5,'learn'] print(x[2]) #15 print(x[-6]) #5 print(x) #[5,10,15,20,25,30] print(x[0:6]) #[5,10,15,20,25,30] print(x[-1:-6]) #Not Working print(y) #[5,'x',10.5,'learn'] print(y[1]) #'x' print(y[-1]) #'learn' print(y[0:3]) #[5,'x',10.5] print(y[-1:--5:-1]) #['learn'10.5,'x',5] (reverse list)
In the above code, we have created two different lists and store them in two different variables named x, y. Now we are going to perform some slicing techniques on these lists. we will use the index to print particular or multiple elements from a list. This slicing method can take three arguments which all are optional. As we can see that we have a list ( x=[5,10,15,20,25,30] ). we have stored all the elements in a list and then stored them in a variable called x. If we want to print the whole list then can do it in multiple ways. like we can skill both indexes and simply can use a colon inside square bracket like ( x[ : ] ). or we can write like x[: index of the last element] or x [ index of the first element: ] ) or x[index of the first element: Index of the last element ]. All the methods will print all the elements of the list. so the first argument inside the square bracket is for starting index of the list which you are going to print and the second argument is for the last index of the list.
List Operations:-
Let’s look at the all-important operations of a list with a brief definition. first, we will see the name of all the operations then we will understand all the operations in detail with code and explanations.
Following are the list of operations in List:-
- Adding
- Replicating
- Slicing
- Update
- Append
- Delete
- Min
- Max
- Len
- Index
- Count
- Pop
- Insert
- Extend
- Remove
- Reverse
- Sort
- Adding:-
we can add to two or more lists by using the concatenation operator(+). The list will be joined horizontally
List1=[5,10,15,20] List2=[1,2,3,4] List3=List1+List2 print(List3) Output:- [5,10,15,20,1,2,3,4]
we have two lists named List1 and List2. and we have to store both lists inside a single list named List3. for that, we used a concatenation operator. this operator will join both lists horizontally.
2. Replication:-
It means Repeating. It can be performed by using a replicating operator(*). And it will print a list multiple times.
List=['Learn','100'] print(List*3] Output:- ['Learn','100','Learn','100','Learn','100']
We have a list of two elements. We multiply this list by the number 3. so it is printing the list three times.
3. List slicing:-
It totally depends on the indexes. It is used to obtain the sub_part of a list.
List1=[5,10,15,20] print(List1[0:2]) #[5,10] print(List1[3]) #20 print(List1[1]) #10
we store a list into a variable named List1. then Print a particular number of items using the slicing method. we have defined the range of the the item using starting index and the end index. if we use [0:2] it will print the data of index 0,1.
4. Update:-
Let’s see that how to update a list.
List1=[5,10,15,20,25] print('before update') Print(List1) List1[1]='Ten' print('after update') print(List1) Output:-'Befote update' [5,10,15,20,25] 'after update' [5,'ten',15,20,25]
As we know we can update a dictionary. because it is a mutable data type. here we printing the data two times. we print data before the update and then again printing after the update. it will print the same list before the update but when we update something. it will change the data of List1 on the basis of keys by which we can update any value of the list. if we print again data after making some changes it will print an updated list. As we can see above. we update the data of index 1 ( List[1] =’Ten’ )
5. Append:-
we will use the append() method to appending an element to a list. It will Append the element at the end of the list
List1=[5,10,15,20,25] print('before append) Print(List1) List1.append(30) print('after append') print(List1) Output='before append' ['5,10,15,20,25] 'after append' ['5,10,15,20,25,30]
we simply append an element at the end of the list. suppose that we are going to buy some vegetables. we have written everything in a list of what we have to buy. but we forgot something to write then we can add more vegetable names at the end of the list. that is called append. The same logic has applied and used this method we append a number at the end of the list.
6. Delete:-
we can delete any element from the list using the del method.
List1=[5,10,15,20,25] print('before delete) Print(List1) del(List1[1]) print('after delete an eletement') print(List1) print('Delete List1') del(List1) Output='before delete' ['5,10,15,20,25] 'after delete an element' ['5,15,20,25] 'Delete list1' NameError: name 'List1' is not defined
In the above code, we are performing three tasks. in the first task, we are making a list and print that list as it is. In the second task, we are deleting an item from the list using index 1 and again printing the list. Now the updated list will be print. and in the third task, we are deleting the whole list. now if we try to print the list it will be showing ‘name ‘List1’ is not defined.
7. Min:-
It will extract the minimum value from the list.
List1=[5,10,15,20,25] min(List1) Output:- 5
Min function is used to extract the minimum element from a list. we got 5 as result. because 5 is the least value in the list.
8. Max:-
It will extract the maximum value from the list
List1=[5,10,15,20,25] max(List1) Output:- 25
Max Function is used to find the maximum element in the list. we got 25 as result. because 25 is the most value.
9. Len:-
it will extract the length of the element of the list
List1=[5,10,15,20,25] len(List1) Output:- 5
Length function is used to find the length of the List. There are 5 elements, as we can count the element manually. so the length of the list is 5. but when we have thousands of elements in a list and then we have to find the length of the list. at that time we will use len() function to find the length of the list.
10. Index:-
I will extract the index value of the object. As we know that in the list every element has an index connect with that we can access every element by using its index.
List1=['learn','sof','techs','com'] print("Index of 'learn",List.index('learn')) Output:-index of 'learn',0
the index is a very important function, sometimes we need the index of a value in a list to perform some task based on the index of the element. For that purpose, we can use the index function.
For example,
we have a list of strings and we want to find the index of a particular string in the list. As we can see above code. we used the index function to find the index of the first string in the list (‘learn’ ,0)
11. Count:-
It returns the number of times an element is repeated in the list.
List1=['learn','sof','techs','com','com'] print("Number of time = ",List1.count('com')) Output:-Number of times = 2
As we can see in the above code there is an element that is repeating two times named ‘com’, Count function will count only repeating elements in the list. that is why we got 2 as result.
12. Pop:-
Returns the last object or the specified indexed object. It will remove the popped object
data=['learn',23.3,'abc','a','786'] print("last element is",data.pop()) print(data) print('second element in data',data.pop(1)) print(data) Output:-'last element is', 786 ['learn',23.3,'abc','a'] 'second element in data',23.3 ['learn','abc','a']
As we know that we can take random data in a list. So we write a list with random data elements. we have to perform the pop operation on this list. The pop function is nothing but the removal of an element from the list. we can remove elements from the list in two ways. 1 using the index 2 without using the index. if we pop an element without using index it will remove the last element of the list. else if we want to remove a particular element with the index it will use an index to find that value and remove it. As we have done in the above code. we use the pop function to remove the last element of the list (‘786) the last element and index to remove particular element(23.3)
13. Insert:-
we can insert an object at the given index using the insert method.
data=['learn',786,45.6,34.5,'com'] data.insert(1,'seven eight six') print(data) Output:- ['learn', 'seven eight six', 786, 45.6, 34.5, 'com']
we are entering an element ‘seven-eight six’ at index 1 using the insert function.
14. Extend:-
It is used to extend the list in a sequence.
List1=['learn',786,45.6,34.5,'com'] List2=['soft',123] List1.extend(List2) print(List1) print(List2) Output:- ['learn',786,45.6,34.5,'com','soft',123] ['soft',123]
Extend function uses to append a list of elements after another list of elements. we are printing two lists List1 has been extended with List2 and List will print the same as before.
15. Remove:-
It removes the object from the given list.
List1=['learn',786,45.6,34.5,'com'] List2=['soft',123] List1.remove(786) print(List1) List2.remove('sof') print(List2) Output:-['learn',45.6,34.5,'com'] [123]
we have two lists named List1 and List2. we have removed ‘786’ from List1 and ‘sof’ from List2 using the remove function.
16. Reverse:-
It reverses the position of all the elements of the list.
List=['learn',786,45.6,34.5,'com'] List.reverse() Output:-['com', 34.5, 45.6, 786, 'learn']
reverse function swap the first element with the last element and increment the index of the loop. all elements could be reversed using a simple reverse function in python.
17. Sort:-
It is used to sort the elements of the list.
List=['learn',786,45.6,34.5,'com'] List.sort() List2=[56,34,56,2,345,67,44,57,60] Output:-[34.5, 45.6, 786, 'com', 'learn']