Python Data Structures — Notebook Notes
File: 03_python_data_structures.ipynb | Topics: List, Set, Tuple, Dictionary
1. List
Create empty list:
empty_cart = []
Create list with items (mixed types):
items_to_buy =
["pen","scale","paper","stabler","stablerpin","stabler","fevistick",5.6,33,
22]
Check type & length:
type(items_to_buy) # list len(items_to_buy) # 10
Indexing:
items_to_buy[0] # "pen" items_to_buy[1] # "scale" items_to_buy[3]
# "stabler" items_to_buy[-8] # "paper"
Slicing:
items_to_buy[0:2] # ['pen', 'scale'] items_to_buy[-7:-2] #
['stabler','stablerpin','stabler','fevistick',5.6]
Update item by index:
items_to_buy[-4] = "watermelon"
2. Set
Create empty set:
empty_cart_1 = set()
⚠ Never use {} for empty set — that creates a dict. Always use set().
Create set with items:
items_to_buy_1 =
{"pen","scale","paper","stabler","stablerpin","stabler","fevistick",5.6,33,
22}
⚠ Duplicate 'stabler' is automatically removed in a set.
Add item:
items_to_buy_1.add("ink")
Remove item:
items_to_buy_1.remove("guava")
⚠ ERROR — 'guava' does not exist in the set. Use .discard() to avoid KeyError.
3. Tuple
Create empty tuple:
empty_card_2 = ()
Tuple with items (note: used list [] instead of tuple () — mistake):
items_to_buy_2 =
["pen","scale","paper","stabler","stablerpin","stabler","fevistick",5.6,33,
22]
⚠ Wrong — used [] (list). Should use () for tuple.
Slicing:
items_to_buy_2[0:2]
Count & Index:
items_to_buy_2.count(items_to_buy_2) # wrong argument
items_to_buy_2.index("stabler") # returns 3
⚠ items_to_buy_2.count(items_to_buy_2) — passing the whole list as argument is wrong. Should pass
a value like .count('stabler').
4. Dictionary
Create empty dict:
empty_card_3 = {}
Dict with items:
items_to_buy_3 =
{"pen":5,"scale":10,"paper":11,"tabler":12,"stablerpin":13,"fevistick":14}
⚠ Typo — 'tabler' should be 'stabler'.
Check undefined variable:
type(price_list)
⚠ ERROR — 'price_list' was never defined. NameError will occur.
Students marks dict:
students_marks = {"maths":94,"english":78,"science":67}
Another dict with errors:
items_to_buy_4 = {"pototo":4, " tomoto":5, "carrot":5}
⚠ Spelling errors: 'pototo' → 'potato', 'tomoto' → 'tomato'. Also leading space in ' tomoto'.
Access undefined variable:
type(empty_card_4) empty_card_4
⚠ ERROR — 'empty_card_4' was never defined here. NameError will occur.
Slice a dict — wrong:
items_to_buy_4[2:4]
⚠ ERROR — Dicts cannot be sliced with [2:4]. Use .keys(), .values(), or list() to iterate.
Clear & Keys:
items_to_buy_4.clear() # removes all items items_to_buy_4.keys() #
returns dict_keys([])
All Mistakes Summary
# Mistake Wrong Code Correct Code
1 Set: .remove('guava') — .remove('guava') .discard('guava')
item not in set
2 Tuple defined with [] items_to_buy_2 = items_to_buy_2 =
instead of () [...] (...)
3 .count() given wrong .count(items_to_ .count('stabler')
argument buy_2)
4 Dict key typo: 'tabler' "tabler":12 "stabler":12
5 Undefined variable: type(price_list) define price_list
price_list first
6 Undefined variable: type(empty_card_ define empty_card_4
empty_card_4 4) first
7 Dict slicing not supported items_to_buy_4[2 list(items_to_buy_4
:4] .keys())[2:4]
8 Spelling in keys: pototo, "pototo", " "potato", "tomato"
tomoto tomoto"