Data Structures

A data structure is a way to store data. Different data structures are used for different tasks. In this lesson, we will learn the core data structures in Python.

List

A list is a data structure that stores ordered data. It also allows you to add and remove data, as well as sort the data. Here is an example of a list containing integers:

numbers = [1, 2, 3]

Here are some basic operations you can do with a list:

numbers = [3, 5, 2]
numbers.append(4) # [3, 5, 2, 4]
numbers.remove(2) # [3, 5, 4]
numbers.sort() # [3, 4, 5]

Exercise

Use list methods to:

  1. Create a list named numbers with the numbers 4, 5, and 6
  2. Given a list named hidden, add 5, remove 2, and then sort.

List Methods Exercise

Loading...

Tuple

A tuple is similar to a list, but it is immutable or unchangeable. Here is an example of a tuple containing integers:

numbers = (1, 2, 3)

This may look familiar to you because function arguments are passed as tuples. Also, if you return multiple values from a function, they are returned as a tuple.

def add_and_subtract(a, b):
    return a + b, a - b

result = add_and_subtract(1, 2)
print(result) # (3, -1)

If you need to modify a tuple, you can convert it to a list, then modify it.

numbers = (1, 2, 3)
numbers = list(numbers)
numbers.append(4)
print(numbers) # [1, 2, 3, 4]

Sequence

list and tuple are both sequences, even strings are sequences. A sequence is an ordered collection of items that can be accessed by index. There are several operations you can do with a sequence.

Indexing

A data element in a sequence can be accessed by its index. The first element in a sequence has an index of 0.

letters = "hello"
numbers = [1, 2, 3]
other_numbers = (4, 5, 6)

print(letters[0]) # h
print(numbers[0]) # 1
print(other_numbers[0]) # 4

print(letters[1]) # e
print(numbers[1]) # 2
print(other_numbers[1]) # 5

Python also has support for negative indexing, which allow you to access elements from the end of the sequence.

letters = "hello"
numbers = [1, 2, 3]
other_numbers = (4, 5, 6)

print(letters[-1]) # o
print(numbers[-1]) # 3
print(other_numbers[-1]) # 6

print(letters[-2]) # l
print(numbers[-2]) # 2
print(other_numbers[-2]) # 5

Slicing

You can also use indexing to create a new sequence from a subset of the original sequence. This is called slicing.

letters = "hello"
numbers = [1, 2, 3]
other_numbers = (4, 5, 6)

print(letters[1:3]) # el
print(numbers[1:3]) # [2, 3]
print(other_numbers[1:3]) # (5, 6)

You don't have to specify the start or end of the slice. If you don't specify the start, it will start at the beginning of the sequence. If you don't specify the end, it will go to the end of the sequence.

numbers = [1, 2, 3]
print(numbers[:2]) # [1, 2]
print(numbers[1:]) # [2, 3]

Iterating

Iterating is the process of going through each element in a sequence. You can do this with a for in loop.

letters = "hello"
for letter in letters:
  if letter == "l":
    print("found l")

In the example above, the variable letter is assigned for each iteration of the loop and can be used to access the current element in the sequence. We can then run specific code for each element in the sequence.

Concatenation

You can also combine two sequences using the + operator. We can see this with strings already, but it also works with lists and tuples.

letters = "hello"
numbers = [1, 2, 3]
other_numbers = (4, 5, 6)

print(letters + ' there!') # hello there!
print(numbers