A list in Python is a mutable and ordered sequence of elements. It is a built-in data structure and is one of several compound data types supported by Python. Each element that is contained in a list is called an item. A list can contain items of different data types which makes lists very powerful. Python lists can also contain duplicate items.
1. Creating a List in Python
A Python list is created by specifying items as comma-separated values inside square brackets [ ].
2. List Indexing
Like strings, lists in Python can also be indexed. Each item of a list corresponds to an index number. The first item has an index 0.
Accessing List Items Using a Positive Index Number
We can use a positive index number in square brackets and access a list item as shown below:
Accessing List Items Using a Negative Index Number
Similar to strings, Python lists also support negative index numbers. We can use negative indices with lists to start counting from the end of the list, starting at the index number -1.
3. Modifying Items in a List Using Index Numbers
Since lists in Python are mutable, we can modify the items in a list. This is done using the assignment operator as shown below:
4. Modifying Lists with + and += Operators
We can concatenate two or more lists together using the + operate as shown below:
The + operator can also be used to add an item or multiple items (in list form) to the end of a list as shown below:
We can also use the += compound operator with lists as shown below:
5. Modifying Lists with * and *= Operators
We can multiply lists using the * operate as shown below:
We can also use the *= compound operator with lists as shown below:
6. Removing an Item from a List
We can use the 'del' statement to remove an item from a list at a specified index number as shown below:
7. Nested Lists
We can create a list which contains another list as an item. A list within another list is called a nested list.
8. List Slicing
Like strings, lists in Python can also be sliced. A slice can be created by using a range of index numbers separated by a colon in the format [x:y]. Here, the first index number is inclusive and specifies where the slice starts from. The second index number is exclusive and specifies where the slice ends.
We can include either end of a list by omitting one of the index numbers. For example, to create a slice that starts at the beginning of the list and ends in the middle, we have to specify only the index number after the colon as shown below:
Likewise, we can create a slice that starts in the middle of the list and includes each item until the end of the list by specifying only the index number before the colon as shown below:
We can also omit both the index numbers to create a slice that starts at the head of the list and ends at the tail of the list. This approach is helpful in combination with the stride parameter which is discussed below in this article.
It is also possible to use negative index numbers for list slicing.
Specifying Stride While Slicing Lists
In the examples above, we have used two index numbers to slice a list. It is also possible to use a third parameter called 'stride' which specifies how many items to move forward after the first item has been retrieved from the list. The default value of stride is 1.
A negative value for stride specifies that we want to extract items from the list in reverse order.
9. List Methods
Lists support a number of methods to perform common operations. It is important to remember that lists are mutable in Python. Therefore, invoking any method on a list will affect the list itself and not a copy of the list. Some of the most commonly used Python list methods are listed below.
It adds a specified item to the end of the list.
It inserts an item at a specified index number. The first argument is the index number at which the item needs to be inserted and the second argument is the item itself.
It removes the first item from the list whose value is equal to the specified value.
Trying to remove an item which is not present in the list results in an error.
It removes the item at the specified index number in the list and returns it. If no index number is specified, it removes and returns the last item in the list.
It returns the index of the first item in the list whose value is equal to the specified value.
It produces an error if there is no item in the list with the specified value.
It returns the number of times the specified value exists in the list.
It is used to sort the items in a list.
It reverses the order of items in a list.
It removes all the items from a list.
It makes a copy of a list. It is helpful in situations when a list needs to be manipulated while still having the original list available to us unchanged.
It is used to combine lists and takes in a second list as its argument.