In the first part, we were introduced to lists and some of the operations that can be performed on a list. You can catch up on it here
This part will be the concluding part on lists. However, there are still a number of concepts not covered such as List Comprehensions, Slicing lists, and Tuples
I have added links to each of these topics that will help to understand these concepts in a simple way.
Removing an item from a list
To remove an item, python has two methods for doing this.
The pop() method: The pops method when used without any index position specified, removes the last item in a list,
>>> groceries = ['milk', 'sugar', 'flour', 'baking soda', 'honey']
>>> groceries.pop()
>>> print(groceries)
>>> ['milk', 'sugar', 'flour', 'baking soda']
But you can also pop any item in your list if you know its position in that list.
>>> groceries = ['milk', 'sugar', 'flour', 'baking soda', 'honey']
>>> groceries.pop(0) #This removes the first item in your list
>>> print(groceries)
>>> ['sugar', 'flour', 'baking soda', 'honey']
The remove() method
This is useful when you do not know the position of the item. Perhaps you now have a large list and cannot keep track of positions.
The remove() method removes the value passed as the parameter
>>> groceries = ['milk', 'sugar', 'flour', 'baking soda', 'honey']
>>> groceries.remove(‘sugar’)
>>> print(groceries)
>>> ['milk', 'flour', 'baking soda', 'honey']
NOTE: If you have more than one item with the same name, the remove method will remove the first appearance of the item in that list.
For example;
>>> cars = [‘bmw’, ‘lexus’, ‘toyota’, ‘bmw’, ‘benz’]
>>> cars.remove(‘bmw’)
>>> print(cars)
>>> [‘lexus’, ‘toyota’, ‘bmw’, ‘benz’] #The new list still has one 'bmw'
The Del operation
We can also delete an item from a list using the del keyword
>>> del cars[0]
>>> del cars[‘toyota’]
However, you have to be careful with the del keyword because it can delete your entire list if you do not pass any value or index position to it
>>> del cars
Modifying a List
Let's take our groceries list and say we do not want to buy baking soda anymore and want to replace it with a different item in your groceries list.
Remember that our groceries list is
groceries = ['milk', 'sugar', 'flour', 'baking soda', 'honey']
To replace baking soda with salt, you just simply input salt at the position of baking soda
groceries[3] = ‘salt’
This will immediately replace whatever item is at index position 3 with baking soda and our new list will be
print(groceries)
['milk', 'sugar', 'flour', 'salt', 'honey']
Sorting Our List
The final method to consider in this series is the sort() method.
Just as the name implies, the sort() method sorts your list in alphabetical order
>>> groceries.sort()
>>> print(groceries)
>>> ['flour', 'honey', 'milk', 'salt', 'sugar']
This method also has the option to reverse the sorted list using the reverse parameter
>>> groceries.sort(reverse=True)
>>> print(groceries)
>>> ['sugar', 'salt', 'milk', 'honey', 'flour']
Conclusion
We have been able to cover the basic concepts of lists in python and you can try some of these operations on your own list.
There are other list concepts such as List Comprehensions, Slicing lists, and Tuples
Let me know if you have any feedback in the comments below. You can also connect with me on Twitter or LinkedIn
Bye 👋 ✌️