Beginner Fundamentals

Lists

A list holds multiple items in order. It’s mutable: you can change it after creation.

fruits = ["apple", "banana", "grape"]

Access

print(fruits[0])    # apple
print(fruits[-1])   # grape
print(fruits[0:2])  # ['apple', 'banana']

Change

fruits[1] = "pear"

Add and remove

fruits.append("mango")     # add to the end
fruits.insert(0, "lemon")  # add at a position
fruits.remove("grape")     # remove by value
fruits.pop()               # remove the last
print(len(fruits))
print("pear" in fruits)

Iterate

for fruit in fruits:
    print(fruit)

Sort

numbers = [3, 1, 2]
numbers.sort()        # [1, 2, 3]
numbers.reverse()     # [3, 2, 1]