In this post we introduce Python lists and operations you can perform on lists in Python.
What is a List in Python?
The Python list is the most basic built-in data type that enables you to store sequences of items in a single variable. For example, we can create a list of letters ‘a’, ‘b’, ‘c’ and store it in a variable called letters. Lists are created using square brackets.
letters = ["a", "b", "c"]
In a list, each item is indexed with a number denoting its position in the list. Python starts counting from 0.
You can access single elements in the list using the index. The following code basically says, go to the zeroth and the second position in the list and retrieve the letters stored there.
letters # returns "a"
letters # returns "c"
What Are The Properties of a List?
Mutable: You can add and remove items from a list.
Ordered: A list will always maintain the order in which you’ve added items to the list
Object-Agnostic: You can append any object to a list ranging from simple numbers to complex custom classes.
Indexed: All items have a unique associated index that can be used to access objects.
Lists can be nested.
Dynamic: If you delete an object from a list, it will shrink. If you add an object, it will grow.
List Functions and List Methods in Python
You can perform various operations on lists such as appending, adding, removing, indexing, slicing, and multiplying, primarily using inbuilt functions.
How to Append to a List in Python
You append a single item at a time to the end of a list using the “append” method on the list.
Rather than appending to the end of a list, you also have the option to add to a list by using the Python insert method. Insert takes the index and the item you want to insert as arguments. It will insert the item before the provided index and dynamically update the indices of the following items.
When you are appending or adding to a list, you are modifying an existing list. But if you want to join a list in Python to another list, you also have the option of concatenating two lists using the plus operator. This will produce a new list.
Here it is easy to see straight away that the letters have been correctly added. Once your lists contain hundreds or thousands of items, determining list length manually is not as straightforward anymore. Luckily, you can calculate the length of a list in Python quite easily.
len(letters) # 6
If you simply want to count items in a Python list, the length function is the way to go.
How to Remove an Element from a List in Python
If you want to remove a specific item from a list, the “remove” method is the most straightforward way. You just specify the image that you want to remove in the method call.
If the elements in your list are all strings, you can use the join function. The join function is called on a character that will separate the list elements in the newly created string. If we simply want to join all letters in a list together, we call join on empty quotation marks.
If you try to do this with a list that includes non-string characters, you’ll get an error.
In this case, you have to convert each element to a string before you apply Python’s join method. Thanks to Python’s list comprehensions, you can do the string conversion and the join in one line of code.
letters = ['b', 'c', 'a', 1, 'd' ]
''.join([str(l) for l in letters]) # 'bca1d'
How to Count Items in a List in Python
To count occurrences of an item in a list, you use the count method. You pass the item whose occurrence you want to count to the method call.
This returns 2 since the letter “a” appears twice in the list.
How to Copy a List in Python
Intuitively, some people might copy a list by simply assigning it to a new variable. But this is problematic because you are only copying the reference to the list. The list itself is not copied. This is known as a shallow copy in Python. In practice, this means both variables still refer to the same list object. If you modify the original list, the copy will also be modified. In this example, we are adding a new letter to the old list. If we print the copy, it also contains the letter, even though we did not explicitly append it to the copy.