A list in Python is an ordered collection of elements, where each element can be of any data type. Lists offer a range of operations that allow us to manipulate and work with the elements stored within them. In this topic, we will explore the basic operations that can be performed on lists in Python. We will learn how to insert elements, check if elements exist, and search for specific elements. Understanding these operations will enable us to effectively work with lists and harness their flexibility for various programming tasks.
Inserting elements at a specified position
As you know, the list.append() method is how we add new elements to the end of a list. If we want to add a new element in the middle, we use the list.insert(position, element) operation. The first argument is the index of the element before which the new element is going to be inserted; so list.insert(0, element) inserts an element to the beginning of the list, and list.insert(len(list), element)is completely similar to list.append(element).
Here is an example:
years = [2016, 2018, 2019]
years.insert(1, 2017) # [2016, 2017, 2018, 2019]
years.insert(0, 2015) # [2015, 2016, 2017, 2018, 2019]
years.insert(len(years), 2020) # [2015, 2016, 2017, 2018, 2019, 2020]
Now, you can fill any empty list with something useful!
Membership testing in a list
Another thing that can be quite useful is checking if an item is present in the list. It can be done simply by using in and not in operators:
catalog = ['yogurt', 'apples', 'oranges', 'bananas', 'milk', 'cheese']
print('bananas' in catalog) # True
product = 'lemon'
print(product in catalog) # False
print(product not in catalog) # TrueSearching specific elements
Sometimes, knowing that the specified element is in the list is not enough; we may want to get more information about it — how many times the element occurs in the list and at which position.
The method count() can help with the quantity:
grades = [10, 5, 7, 9, 5, 10, 9]
print(grades.count(5)) # 2
We can use the method index() to get the position of the element. It finds the index of the first occurrence of the element in the list:
print(grades.index(7)) # 2
print(grades.index(10)) # 0
We can also specify the interval for searching: list.index(element, start, end).
print(grades.index(9, 2, 5)) # 3
# if we don't specify the end of the interval, it automatically equals the end of the list
print(grades.index(10, 1)) # 5
end index is not included in the interval.
It is also good to know that if the element we are looking for is not in the list, the method will cause an error:
print(grades.index(8)) # ValueError: 8 is not in listSummary
In conclusion, we have covered more operations related to manipulating and searching elements in a list. We have learned how to insert elements at a specified position using the list.insert(position, element) operation, allowing us to add elements anywhere in the list. Additionally, we explored membership testing using the in and not in operators, which helps us determine if a particular item is present in the list.
Furthermore, we discussed the count() method, which provides us with the number of occurrences of a specific element in the list. The index() method allows us to find the position of the first occurrence of an element, and we can even specify an interval for searching.
By understanding these operations, we have expanded our ability to manipulate and search elements in Python lists. If you require further information, it is recommended to consult the Python Data Structures documentation for a more comprehensive understanding of these topics.