Python Tips: How to Use Lists

Knowing how to use lists is essential for any Python developer as they have many real-world coding applications. In this tutorial, we’ll dive deep into the list class and get a solid understanding of its key features.
To get started with using lists in Python, you can create new ones by assigning values separated by commas to a variable:

my_list = [1, 2, ‘three’, True]
print(my_list) # prints “[1, 2, ‘three’, True]”

You can access the items in an existing list using their index. The first item has an index of zero (0), and so on:

my_list = [1, 2, ‘three’, True]
print(my_list[1]) # prints “2”

To copy or update a list, you can use the slicing operator `[:]`, which creates a shallow copy of an existing list. A shallow copy is a new list containing references to the objects stored in the original list:

my_list = [1, 2, ‘three’, True]
new_list = my_list[:] # create a shallow copy of my_list
print(id(my_list), id(new_list)) # prints “43807968 43807968” (same object ID)

To update an item in a list, you can use the index to access it and then assign a new value:

my_list = [1, 2, ‘three’, True]
my_list[1] = 42 # updates the second item with the value of 42
print(my_list) # prints “[1, 42, ‘three’, True]”

To grow or shrink a list, you can use the `append()`, `insert()`, and `remove()` methods:

my_list = [1, 2, ‘three’]
my_list.append(4) # adds an item to the end of the list
print(my_list) # prints “[1, 2, ‘three’, 4]”
my_list.insert(1, 37)

In Python, youll find a few other built-in and standard-library functions that allow you to traverse a list of values and obtain a final result either as another list, an iterator, or even a single value. Some examples include reduce(), min() and max(), sum(), all(), and any(). Note that some of these functions arent really functional programming tools, but they internally iterate over the input list.

Exploring Other Features of Lists
Python has a few tools that allow you to search for values in an existing list. For example, if you only need to quickly determine whether a value is present in a list, but you dont need to grab the value, then you can use the in or not in operator, which will run a membership test on your list object.
The first expression allows you to determine if item is in list_object. It returns True if it finds item in list_object or False otherwise. The second expression works in the opposite way, allowing you to check if item is not in list_object. In this case, you get True if item doesnt appear in list_object.
Heres how membership tests work in practice:

my_list = [1, 2, ‘three’, True]
print(3 in my_list) # prints “False”
print(‘two’ in my_list) # prints “False”
print(‘three’ in my_list) # prints “True”

In the above example, we have a list `my_list`. We are checking whether 3 and ‘two’ exist in this list. Since 3 is not present in the list, it returns False

You can also use comprehensions to filter existing lists. For example, say that you have a list of integer values and want to create a new list containing only the even values out of your original list:
The if clause in this list comprehension works as a filter that selects only the even numbers from your original data. How would you write a similar comprehension to retrieve the odd numbers?

You can also take advantage of some Python functional programming tools, such as map() and filter(), to traverse a list of values. These functions have an internal loop that iterates over the items of an input iterable and returns a given result.
For example, the map() function takes a transformation function and an iterable as arguments. Then it returns an iterator that yields items that result from applying the function to every item in the iterable.
Using map(), you can convert your list of numbers to integers with the following code:
In this example, map() applies int() to every item in numbers in a loop. Because map() returns an iterator, youve used the list() constructor to consume the iterator and show the result as a list.
If you need to filter values from an existing list, then you can use the built-in filter() function. This function takes two arguments: a predicate function and an iterable of data. Then it returns an iterator that yields items that meet a given condition, which the predicate function tests for.
Heres how filter() works in practice:
In this example, you use filter() to traverse your integers list and extract those values that satisfy the condition of being even numbers.

Deciding Whether to Use Lists
As you’ve learned throughout this tutorial, lists are powerful, flexible, versatile, and full-featured data structures. Because of their characteristics, people tend to use and abuse them. Yes, theyre suitable for many use cases, but sometimes they arent the best available option.
In general, you should use lists when you need to:

Keep your data ordered: Lists maintain the order of insertion of their items. Store a sequence of values: Lists are a great choice when you need to store a sequence of related values. Mutate your data: Lists are mutable data types that support multiple mutations. Access random values by index:

However, keep in mind that lists can become slow and memory-intensive if they grow too large or contain many duplicates. In these cases, you may want to consider using other data structures such as sets or dictionaries instead of lists for better performance.

SICORPS