Introduction to List Operations in Python
List is a type of data structuring method that allows storing of the integers or the characters in an order indexed by starting from 0. List operations are the operations that can be performed on the data in the list data structure. A few of the basic list operations used in Python programming are extend(), insert(), append(), remove(), pop(), slice, reverse(), min() & max(), concatenate(), count(), multiply(), sort(), index(), clear(), etc.
myList = [1, 2, 3, ‘EduCBA’, ‘makes learning fun!’]
List Operations in Python
Given below are some of the most widely used list operations in Python:
1. append()
The append() method is used to add elements at the end of the list. This method can only add a single element at a time. To add multiple elements, the append() method can be used inside a loop.
Code:
myList.append(4)
myList.append(5)
myList.append(6)
for i in range(7, 9):
myList.append(i)
print(myList)
Output:
2. extend()
The extend() method is used to add more than one element at the end of the list. Although it can add more than one element, unlike append(), it adds them at the end of the list like append().
Code:
myList.extend([4, 5, 6])
for i in range(7, 9):
myList.append(i)
print(myList)
Output:
3. insert()
The insert() method can add an element at a given position in the list. Thus, unlike append(), it can add elements at any position, but like append(), it can add only one element at a time. This method takes two arguments. The first argument specifies the position, and the second argument specifies the element to be inserted.
Code:
myList.insert(3, 4)
myList.insert(4, 5)
myList.insert(5, 6)
print(myList)
Output:
4. remove()
The remove() method is used to remove an element from the list. In the case of multiple occurrences of the same element, only the first occurrence is removed.
Code:
myList.remove('makes learning fun!')
myList.insert(4, 'makes')
myList.insert(5, 'learning')
myList.insert(6, 'so much fun!')
print(myList)
Output:
5. pop()
The method pop() can remove an element from any position in the list. The parameter supplied to this method is the index of the element to be removed.
Code:
myList.pop(4)
myList.insert(4, 'makes')
myList.insert(5, 'learning')
myList.insert(6, 'so much fun!')
print(myList)
Output:
6. slice
The slice operation is used to print a section of the list. The slice operation returns a specific range of elements. It does not modify the original list.
Code:
print(myList[:4]) # prints from beginning to end index
print(myList[2:]) # prints from start index to end of list
print(myList[2:4]) # prints from start index to end index
print(myList[:]) # prints from beginning to end of list
Output:
7. reverse()
The reverse() operation is used to reverse the elements of the list. This method modifies the original list. To reverse a list without modifying the original one, we use the slice operation with negative indices. Specifying negative indices iterates the list from the rear end to the front end of the list.
Code:
print(myList[::-1]) # does not modify the original list
myList.reverse() # modifies the original list
print(myList)
Output:
8. len()
The len() method returns the length of the list, i.e. the number of elements in the list.
Code:
print(len(myList))
Output:
9. min() & max()
The min() method returns the minimum value in the list. The max() method returns the maximum value in the list. Both the methods accept only homogeneous lists, i.e. lists having elements of similar type.
Code:
print(min(myList))
Output:
Code:
print(min([1, 2, 3]))
print(max([1, 2, 3]))
Output:
10. count()
The function count() returns the number of occurrences of a given element in the list.
Code:
print(myList.count(3))
Output:
11. concatenate
The concatenate operation is used to merge two lists and return a single list. The + sign is used to perform the concatenation. Note that the individual lists are not modified, and a new combined list is returned.
Code:
yourList = [4, 5, 'Python', 'is fun!']
print(myList+yourList)
Output:
12. multiply
Python also allows multiplying the list n times. The resultant list is the original list iterated n times.
Code:
print(myList*2)
Output:
13. index()
The index() method returns the position of the first occurrence of the given element. It takes two optional parameters – the beginning index and the end index. These parameters define the start and end position of the search area on the list. When supplied, the element is searched only in the sub-list bound by the begin and end indices. When not supplied, the element is searched in the whole list.
Code:
print(myList.index('EduCBA')) # searches in the whole list
print(myList.index('EduCBA', 0, 2)) # searches from 0th to 2nd position
Output:
14. sort()
The sort method sorts the list in ascending order. This operation can only be performed on homogeneous lists, i.e. lists having elements of similar type.
Code:
yourList = [4, 2, 6, 5, 0, 1]
yourList.sort()
print(yourList)
Output:
15. clear()
This function erases all the elements from the list and empties them.
Code:
myList.sort()
print(myList)
Output:
Conclusion
List operations are strategically essential to learn how to perform a task with minimal lines of code. This is just a basic introduction to list operations covering most of the methods. It is recommended to play around more, get creative and explore the potential of lists further.
Recommended Articles
This is a guide to List Operations in Python. Here we discuss the introduction and most widely used list operations in python with code and output. You can also go through our other suggested articles to learn more –
40 Online Courses | 13 Hands-on Projects | 215+ Hours | Verifiable Certificate of Completion
4.8
View Course
Related Courses