Python: Lists & Dictionaries
What you will learn
How to work with ordered collections (lists) and key-value mappings (dictionaries).
Lists (ordered, mutable)
fruits = ["apple", "banana", "cherry"]
fruits.append("date") # add to end
fruits.insert(1, "apricot") # insert at index 1
fruits.pop() # remove and return last
fruits.sort() # sort in-place
len(fruits) # number of elements
fruits[0] # first element
fruits[-1] # last element
Lists are zero-indexed. Negative indices count from the end.
Dictionaries
user = {
"name": "Alice",
"age": 25,
"skills": ["Python", "SQL"]
}
print(user["name"]) # "Alice"
user["lang"] = "Python" # add new key
user.get("missing", "N/A") # safe access with default
del user["age"] # remove key
user.keys() # view all keys
Quick check below!