“Python List Manipulation: 10 Proven Methods for Boosting Your Skills”
As we know a list in Python is a collection of items that can be organized in a specific order. You can change, add, or remove things from the list even after it has been created. Lists can contain any type of data, such as numbers, strings, or even lists. They are very useful in Python because they are flexible and can be used in many ways.
Before we start the list methods, we need to know some of the properties and characteristics of lists in Python, as follows:
- Lists maintain the order of elements, i.e. the first element in a list is the first element, the second element is the second element and so on.
- Lists can be modified after they are created. You can add, remove, or change items in a list.
- Each element in the list is assigned an index, which always starts at 0 for the first element in the list
- The list can be of any length and contain any number of elements
- The list can contain elements of any data type.
So let us look at some of the most commonly used methods in Python
append(item):
Adds an element to the end of the list
my_list = [1, 2, 3, 4]
my_list.append(5)
print(my_list) #=====> Output: [1, 2, 3, 4, 5]
insert(index,item):
Inserts an item at the specific index
my_list = [1, 2, 4, 5]
my_list.insert(2, 3) # my_list.insert(index,item)
print(my_list) #=====> Output: [1, 2, 3, 4, 5]
remove(item):
Removes the first occurrence of the specified item from the list.
my_list = [1, 2, 3, 4]
my_list.remove(3)
print(my_list) #=====> Output: [1, 2, 4]
pop():
Used to remove and return the last element from a list. The method takes an optional argument, namely the index of the element to be removed from the list. If no index is given, the last element is removed from the list.
days = ['Monday', 'Tuesday', 'Friday', 'Saturday']
# remove and return the last item from the list
last_day = days.pop()
print(days) #=====> Output: ['Monday', 'Tuesday', 'Friday', 'Saturday']
print(last_day) #=====> Output: Saturday
index(item):
Returns the index of the first occurrence of the specified element in the list
my_list = [1, 2, 3, 2]
index_of_two = my_list.index(2)
print(index_of_two) #=====> Output: 1
count(item):
Returns the number of times the specified element appears in the list.
my_list = [1, 2, 2, 3, 2]
count_of_two = my_list.count(2)
print(count_of_two) #=====> Output: 3
sort()
- Sorts the elements in the list in ascending order.
my_list = [4, 2, 1, 3]
my_list.sort()
print(my_list) #=====> Output: [1, 2, 3, 4]
reverse()
- Reverses the order of the element in the list.
my_list = [1, 2, 3, 4]
my_list.reverse()
print(my_list) #=====> Output: [4, 3, 2, 1]
copy()
- Returns a copy of the list.
my_list = [1, 2, 3, 4]
my_list_copy = my_list.copy()
print(my_list_copy) #=====> Output: [1, 2, 3, 4]
extend(list)
- Adds the elements from another list to the end of the list.
my_list = [1, 2, 3]
my_list.extend([4, 5, 6])
print(my_list) #=====> Output: [1, 2, 3, 4, 5, 6]
In short, when using list in Python, there are a few recommendations to keep in mind. First, lists can be modified, so you need to be careful when making changes to them. If you make a change to a list, it can affect other parts of your code. Second, you can use indexes to find and work with items in a list. However, you must be careful not to try to find an element that does not exist, because then your code will not work. Finally, you can use special methods that Python has developed for working with lists. These methods make it easier and faster to work with lists.
Thanks for reading and I hope this article helps you! 😊