1. Briefly about lists
A list is an ordered collection of different types of data.
The data in a Python list can be of any type. For example, the list in Python can contain both integer data and string values.
The elements in the list are in order, and their position is determined by the index. The index of the first position in the list is 0.
# Create lists
list = [1, 2, 3, 4, 7, 9]
list2 = [1, ‘str’, True, [1, 2]]
2. Basic work with elements
Python allows you to access any element in a list and set the value of the elements using square brackets.
# Get a value from list
number = list[1]
# Set value
list[0] = 3
3. Slicing in Python Lists
Slice notation in Python lists are some of the elements in the list that start and end at specific positions. Slice allows you to specify which element to start and finish extracting data from the list. This will return a new list of retrieved items.
list[start, stop, step]
start – index of the element from which the extraction starts, by default it is 0;
stop - index of the end element. If not specified, then the slice is taken up to the last element;
step - sampling step. A negative value allows elements to be retrieved in reverse order.
list[2:4] -> [3, 4]
4. How to find out if there is an element in the list: the in operator
The in operator in Python allows you to check if the element you are looking for is in a list. This operator iterates through the list, iterating over each element. If the list is large, then this can take a long time.
list = [1, 2, 3, 4, 7, 9]
7 in list # True
23 in list # False
5. Combining lists. Concatenation
Lists in Python can be easily connected to each other using an extension method or simply adding a list using the '+' operator
# Using extend method
list.extend([45, 56, 67])
# Concatenation
list3 = list + list2
6. Add an element to the list.
Python also provides list methods for adding new elements to a list. They are append and insert.
The append method adds a new element to the end of the list. This new element is passed as an argument to the append method.
list.append(67)
The type of the passed element can be anything. The method itself returns None. The list will be automatically enlarged by adding a new element. Unlike other programming languages, Python does this for us.
Python also has an insert method that inserts a new element into a list at a given index position. The index is passed as the first argument to the insert method, and the new element is passed as the second argument. The main thing to remember is that the positions of elements in the list start from 0, and the type of the passed element can be anything. The index type must be integer.
list.insert(4, 56)
Both of this methods modify the list on which they are called and do not return anything.
7. Removing elements from a list using Python
Python has a method for removing an element from a list by its value, not by index. This is the remove method. This method accepts an argument that must match the value to be removed.
For example, we have a list numbers = [1, 2, 5, 6]. And we want to remove the number 5 from this list. We can use the remove method: numbers.remove(5) . Now the number 5 will be missing from the list, because the remove method removed this element by its value.
If you pass a non-existent value in the list to the argument, then a ValueError exception will be raised.
This method returns None and modifies the list it was called on.
Python has another method to remove elements. This is the pop method. This method takes an optional one argument and is the index of the element to be removed. The pop method returns the element to be removed. If you do not pass the index of the element to be removed, then this method removes the last element in the list.
# Remove element at position 1
list.pop(1)
# Removing the last element
list.pop()
The pop and remove methods modify the list they were called on.
In conclusion, I would like to note that all the necessary operations with lists are already built into Python, which cover most of the cases in work, which makes working with lists very convenient.
Top comments (0)