Python Lists 101: Everything You Need to Know

Per aspera ad astra
4 min readJan 23, 2023

--

Introduction:

Python lists are one of the most fundamental data structures in the language. They allow you to store and organize multiple items in a single place, making them an essential tool for any programmer. In this lesson, we will be diving deep into the various list functions available in Python, including how to use them and when to use them. By the end of this lesson, you should have a solid understanding of how to manipulate lists in Python and be able to use them to solve real-world problems.

List Creation:

Before we can start working with lists, we first need to know how to create them. In Python, there are several ways to create lists, but the most common is to use square brackets [] with a comma-separated list of items. For example:

my_list = [1, 2, 3, 4, 5]

This creates a list called “my_list” that contains the numbers 1 through 5. You can also create a list with no items by using empty square brackets:

empty_list = []

List Access:

Once you have a list, you will likely want to access the items within it. You can access items in a list by using their index, which is a zero-based number that represents their position in the list. For example, to access the first item in “my_list”, you would use the following code:

first_item = my_list[0]

This would set the variable “first_item” to the value 1. You can also use negative indexes to access items from the end of the list. For example, to access the last item in “my_list”, you would use the following code:

last_item = my_list[-1]

This would set the variable “last_item” to the value 5.

List Slicing:

In addition to accessing individual items in a list, you can also access a range of items using slicing. Slicing allows you to extract a portion of a list by specifying a start and end index. For example, to extract the first three items in “my_list”, you would use the following code:

first_three = my_list[0:3]

This would create a new list called “first_three” that contains the values [1, 2, 3]. The start index is inclusive and the end index is exclusive.

List Modification:

Once a list is created, you can modify its contents by reassigning individual items or by using methods such as append(), insert(), remove(), pop(), clear().

# Reassigning individual items
my_list[0] = 0
# append() method
my_list.append(6)
# insert() method
my_list.insert(3, -1)
# remove() method
my_list.remove(3)
# pop() method
my_list.pop(2)
# clear() method
my_list.clear()

List Concatenation:

You can concatenate two or more lists together by using the “+” operator. For example, to concatenate “my_list” and “first_three”, you would use the following code:

new_list = my_list + first_three

This creates a new list called “new_list” that contains all of the items from both “my_list” and “first_three”.

List Iteration:

You can iterate over the items in a list using a for loop. For example, to print out each item in “my_list”, you would use the following code:

for item in my_list:
print(item)

You can also use the built-in function enumerate() to get the index and value of each item in a list as you iterate over it. For example:

for index, value in enumerate(my_list):
print(index, value)

List Comprehension:

List comprehension is a powerful feature in Python that allows you to create a new list by applying an operation to each item in an existing list. It’s a concise and readable way to create a list in a single line of code.

For example, to create a new list that contains the squares of all the items in “my_list”, you would use the following code:

squared_list = [item ** 2 for item in my_list]

You can also add a conditional statement to a list comprehension to filter items based on a certain condition. For example, to create a new list that contains only the even numbers from “my_list”, you would use the following code:

even_list = [item for item in my_list if item % 2 == 0]

List Methods:

There are several built-in methods that you can use to manipulate lists in Python. Some of the most commonly used methods include:

  • len(): Returns the number of items in a list.
  • count(): Returns the number of times an item appears in a list.
  • sort(): Sorts the items in a list in ascending order.
  • reverse(): Reverses the order of the items in a list.
  • index(): Returns the index of the first occurrence of an item in a list.

For example, to find the number of times the number 3 appears in “my_list”, you would use the following code:

count = my_list.count(3)

Conclusion:

In this lesson, we have covered the basics of working with lists in Python, including how to create and access lists, how to modify and concatenate lists, and how to use list iteration and list comprehension. We also looked at some of the most commonly used list methods, such as len(), count(), sort(), reverse(), and index(). By now you should have a solid understanding of how to manipulate lists in Python and be able to use them to solve real-world problems.

--

--