Skip to main content
eLearner.app
Module 3 · Lesson 1 of 49/36 in the course~12 min
Module lessons (1/4)

Lists

A list is an ordered and mutable sequence of elements. It is the most used data structure in Python: you create one with [] and modify it with a set of methods worth memorizing.

Creating a list

Python
vuota = []
numeri = [10, 20, 30]
mista = [1, "due", 3.0, True, None]    # tipi diversi: legale ma raro

Access by index and slice

Indexes start at 0. Negative indexes count from the right:

Python
parole = ["a", "b", "c", "d"]
parole[0]    # 'a'
parole[-1]   # 'd'   (ultimo)
parole[1:3]  # ['b', 'c']   slice [start:stop]
parole[::-1] # ['d', 'c', 'b', 'a']   reverse

A slice never raises IndexError: if it is out of range, it returns an empty sub-list. Direct access like parole[10], instead, raises an exception.

Modifying a list

Python
nums = [1, 2, 3]
nums.append(4)         # [1, 2, 3, 4]    aggiunge in fondo
nums.insert(0, 0)      # [0, 1, 2, 3, 4] inserisce in posizione
nums.extend([5, 6])    # [0, 1, 2, 3, 4, 5, 6]   concatena
nums.pop()             # 6   estrae ultimo
nums.pop(0)            # 0   estrae primo (più lento: O(n))
nums.remove(3)         # rimuove la PRIMA occorrenza di 3
nums[1] = 99           # sostituisce per indice

Sorting

Two forms — it's important to tell them apart:

Python
nums = [3, 1, 4, 1, 5, 9, 2, 6]
nums.sort()          # MUTA nums in place, ritorna None
ordinati = sorted(nums)  # ritorna NUOVA lista, lascia nums invariata
nums.sort(reverse=True)
sorted(nums, key=lambda n: -n)   # ordinamento per chiave

Length, membership, iteration

Python
len(nums)              # quanti elementi
3 in nums              # True/False
for n in nums:
    print(n)

Copying a list

a = b does not copy — it just creates a second name for the same list. To actually copy:

Python
copia = lista[:]            # slice completo
copia = list(lista)         # costruttore esplicito
copia = lista.copy()        # metodo

(These are all shallow copies — nested elements remain shared.)

Copying lists: watch out for references

In Python, when you write list_b = list_a, you are not creating a copy: you are just creating a second reference. Modifying list_b will alter list_a as well. To create a true independent copy, you must write list_b = list_a.copy() or use full slicing: list_b = list_a[:].

Try it

Exercise#python.m3.l1.e1
Attempts: 0Loading…

Given the list `nums = [5, 2, 8, 1, 9, 3]`, build `first_three_sorted` with the first three numbers of nums sorted in ascending order. Do not modify nums.

Loading editor…
Show hint

nums[:3] are the first three, then sorted(...).

Solution available after 3 attempts

Review exercise

Exercise#python.m3.l1.e2
Attempts: 0Loading…

Starting from `words = ['ciao']`, append 'mondo' at the end, then insert '!' at the beginning. Evaluate `words`.

Loading editor…
Show hint

append at the tail, insert(0, ...) at the head.

Solution available after 3 attempts

Additional challenge

Exercise#python.m3.l1.e3
Attempts: 0Loading…

Given the list `items = [10, 20, 30, 40]`, use slicing to create a new list `reversed_items` containing the same elements but in reverse order. Finally, evaluate `reversed_items`.

Loading editor…
Show hint

Slicing with a negative step is [::-1]. Assign it to reversed_items.

Solution available after 3 attempts