DEV Community

Lourenço Costa
Lourenço Costa

Posted on

Python course: Lists

This is one of the most typical ways in Python to store collections of values.

winners = ["dwight", "darryl", "angela", "kevin", "phyllis", "toby", "oscar"]
crazy_list = ["banana", 1, True, ["A", "B"], 50.0, {}]
Enter fullscreen mode Exit fullscreen mode

As seen in crazy_list, a list may hold values of different types.

Accessing elements in a list

A crucial concept to grasp in order to work with lists is "indexes". Each element in a list is automatically assigned a number, starting at 0, according to their position.

So in the winners list:

Element Index Negative index
dwight 0 -7
darryl 1 -6
angela 2 -5
kevin 3 -4
phyllis 4 -3
toby 5 -2
oscar 6 -1

With that in mind, you can access individual elements by referring to their indexes:

print(winners[0]) # => dwight
print(winners[1:4]) # => ['darryl', 'angela', 'kevin']
print(winners[-1]) # => oscar
print(winners[:3]) # => ['dwight', 'darryl', 'angela']
print(winners[4:]) # => ['phyllis', 'toby', 'oscar']
Enter fullscreen mode Exit fullscreen mode

A few notes:

winners[1:4]

Note that the syntax for slicing a list is <list>[<fist index> : <last index>]. The first index is inclusive, and the last index is exclusive. What it means in practice is that "1:4" actually ranges the elements starting at index 1 and finishing at index 3 (4-1). This characteristic is inherited from the mathematical concept of intervals. In this case, the first index 1 is a closed (inclusive) interval, and the second index 4 is an open (exclusive) interval. See below a visual representation of this idea:

1 4
[ x ] [ ]

winners[-1]

By using a negative number, the index number is counted backwards, where -1 means the last element in the list. So, in this case "oscar" can be accessed by both indexes 6 and -1, "toby" by 5 and -2, and so on.

Being able to select the negative index is very convenient. Let's say you have a huge list and you wish to access its last element. Instead of visually checking its position, you can simply select the index -1.

winners[:3]

Omitting the first index is a shortcut for the first index (0), so this is the same as 0:3.

winners[4:]

Omitting the last index is a shortcut for the last index (-1), so this is the same as 4:-1.

Validation if a list has elements

This concept assumes you have read both Functions and Conditionals posts.

Consider the following situation: you have a function that receives a list. If the list has elements, the function performs action "A", but if the list is empty, then it performs action "B".

You may use the len() function to get the number of elements in a list.

winners = ["dwight", "pam", "angela", "kevin", "phyllis", "toby", "oscar"]
losers = []

print(len(winners)) # => 7
print(len(losers)) # => 0
Enter fullscreen mode Exit fullscreen mode

The len() function can be used with other types too, such as dicts and strings.

It's very common for people to assume you need to check whether the quantity of elements in the list is greater than 0 ( len(winners) > 0 ) to accomplish that. But in Python there's a convenient abstraction for checking whether a list has elements or is empty:

winners = ["dwight", "pam", "angela", "kevin", "phyllis", "toby", "oscar"] 
losers = []

def handle_list(some_list: list) -> None: 
    if some_list:
        print("list has values") 
    else:
        print("list is empty")

handle_list(winners) # => list has values 
handle_list(losers) # => list is empty
Enter fullscreen mode Exit fullscreen mode

List comprehension

This concept assumes you have read the Loops post.

This is an interesting feature in Python that allows you to create lists using a more concise and readable syntax:

winners = ["dwight", "pam", "angela", "kevin", "phyllis", "toby", "oscar"]

winners_uppercase = [i.upper() for i in winners]
print(winners_uppercase) #=> ['DWIGHT', 'PAM', 'ANGELA', 'KEVIN', 'PHYLLIS', 'TOBY', 'OSCAR']

winners_containing_letter_a = [i for i in winners if "a" in i]
print(winners_containing_letter_a) #=> ['pam', 'angela', 'oscar']

# Another interesting example is creating a list of numbers...
numbers_from_1_to_5 = [i for i in range(1, 6)]
print(numbers_from_1_to_5) # => [1, 2, 3, 4, 5]

# ...and creating a list with the alphabet letters:
alphabet_letters = [chr(i) for i in range(ord("A"), ord("B") + 25)]
print(alphabet_letters) #=> ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
Enter fullscreen mode Exit fullscreen mode

Explaining:

winners_uppercase

This is a list created with list comprehension. Let's go over its details in two parts:

  1. Defines what will be in the new list: i.upper()
  2. Defines the iteration over the original list: for i in winners

winners_containing_letter_a

Also a list created with list comprehension. There's a third part now:

  1. Defines what will be in the new list: i
  2. Defines the iteration over the original list: for i in winners
  3. Defines a conditional for i to be added to the new list: if "a" in i(if the letter "a" is found in i)

Some methods in lists

There are many built-in methods to extend the capabilities of a list. Visit here to see them all. Next, a few of the most popular ones:

winners = ["dwight", "pam", "angela", "kevin", "phyllis", "toby", "oscar"]

winners.pop(1) # The element at index 1 ("pam") was removed from the list

winners.append("jim") # Now "jim" is included in the list

winners.sort() # Now the list is in alphabetical order

print(winners) # => ['angela', 'dwight', 'jim', 'kevin', 'oscar', 'phyllis', 'toby']
Enter fullscreen mode Exit fullscreen mode

😊 Enjoying this series? The full book contains even more content! Support my work by purchasing the complete book in digital or paperback formats. Click below to find out more.
Alt text


Follow me around:
LinkedIn Buy me a coffee GitHub Dev.to

Top comments (0)