Lists in Python (11/100 Days of Python)

Martin Mirakyan
3 min readJan 12, 2023

--

Day 11 of the “100 Days of Python” blog post series covering lists

Lists are an essential data structure in Python, and they are used to store an ordered collection of items. They are similar to arrays in other programming languages, but they have a few unique features. In this tutorial, we will learn how to use lists in Python, including how to create them, access their elements, and modify them.

Creating Lists

There are a few different ways to create lists in Python. The most straightforward way is to enclose a comma-separated sequence of values in square brackets []. For example, to create a list of integers from 1 to 10, you can use the following code:

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

You can also create a list by assigning it to a variable and using the list() function. This is useful when you want to create a list from an existing iterable object, such as a string or a tuple. For example:

# create a list from a string
chars = list('hello')
print(chars) # ['h', 'e', 'l', 'l', 'o']

Finally, you can use multiplication to create a list of the same value:

zeros = [0] * 10
print(zeros) # [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

Indexing Lists

Once you have a list, you can access its elements using indexing. In Python, indexing starts at 0, so the first element of a list is at index 0, the second element is at index 1, and so on. You can access an element of a list by enclosing the index in square brackets [] after the list name:

numbers = [1, 2, 3, 4, 5]
print(numbers[0]) # 1 (the first element)
print(numbers[2]) # 3 (the third element)

You can also use negative indices to access elements from the end of the list. The last element of the list is at index -1, the second-to-last element is at index -2, and so on:

numbers = [1, 2, 3, 4, 5]
print(numbers[-1]) # 5 (the last element)
print(numbers[-2]) # 4 (second-last element)

Adding elements to a list with the append method

There are several methods you can use to modify lists in Python. One of the most common is the append() method. The append() method allows you to add an item to the end of a list. It is a very useful method for building lists dynamically:

names = ['Anna', 'Bob']
print(names) # ['Anna', 'Bob']

names.append('Simon') # ['Anna', 'Bob', 'Simon']
print(names)

names.append('Lily')
print(names) # ['Anna', 'Bob', 'Simon', 'Lily']

So, you can use the append() method multiple times to add multiple items to the list.

Removing elements from a list with del

The del statement is a powerful way to remove items from a list. You can use it to remove a specific item by its index, or to remove a range of items:

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

del my_list[2] # remove the third item (index 2)
print(my_list) # prints [1, 2, 4, 5]

You can also use the del statement to remove a range of items from a list using the range syntax del my_list[start: end]. This will remove all items from the list with an index between start and end, not including the item at end:

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

# remove the second and third items (index 1 and 2)
del my_list[1:3] # Remove [1, x, x, 4, 5]
print(my_list) # prints [1, 4, 5]

What’s next?

--

--

Martin Mirakyan
Martin Mirakyan

Written by Martin Mirakyan

Software Engineer | Machine Learning | Founder of Profound Academy (https://profound.academy)

No responses yet