๐ Python Lists: Your Digital Shopping Basket

When youโre out shopping, you probably donโt carry each item in your hands โ you use a basket. Thatโs what lists are in Python โ containers that hold a bunch of stuff under one name.
๐งบ Letโs Pack a List
Want to store some dogsโ names?
dogs = ["Roger", "Syd"]
Boom. Thatโs a list. You can keep mixing things up too:
items = ["Roger", 1, "Syd", True]
Lists are chill โ they donโt care if you mix strings, numbers, or booleans.
๐ Searching Inside Your List
Want to check if "Roger" is chilling in items?
print("Roger" in items)  # True
Python politely answers: โYes, boss.โ
๐ข Indexing: Numbers Matter
Lists start counting at zero (not one). That means:
items[0]  # "Roger"
items[1]  # 1
items[-1] # True (last item)
Need to change something?
items[0] = "Roggy"
Now "Roger" is "Roggy".
๐ฏ Finding Item Positions
Where is "Syd"?
items.index("Syd")  # 2
Indexes help you grab or target items โ like GPS for your list.
โ๏ธ Slicing: Cut Out What You Need
Slice like a pro:
items[0:2]  # ['Roger', 1]
items[2:]   # ['Syd', True]
Youโre not copying โ youโre slicing.
๐ How Big Is Your List?
len(items)  # 4
Size check complete.
โ Adding Items
Three ways to add stuff:
items.append("Test")
items.extend(["Test"])
items += ["Test"]
โ ๏ธ Careful here:
items += "Test"  # BAD! Youโll get ['T', 'e', 's', 't']
Use square brackets!
โ Removing Stuff
Want to kick "Test" out?
items.remove("Test")
Gone. Like it was never there.
๐ฏ Insert Anywhere
items.insert(1, "Test")  # Adds "Test" at position 1
Multiple items?
items[1:1] = ["Test1", "Test2"]
Nice trick, right?
๐งน Sorting Things Out
Want things in order?
items.sort()
But careful โ you canโt mix numbers and strings here.
To fix letter cases:
items.sort(key=str.lower)
Want to keep the original and just see a sorted version?
sorted_items = sorted(items, key=str.lower)
print(sorted_items)
๐ง Practice Time!
Try these 5 questions below ๐
1.
Create a list with the following items: "Apple", "Banana", 3, and False.
Print the length of the list.
2.
Check if "Banana" exists in the list. If it does, print its index.
3.
Change "Apple" to "Mango" using indexing.
4.
Add "Orange" and "Pineapple" to the end of the list.
5.
Sort only the string values in the list. (Hint: Separate strings first or make a new list with only strings.)
Lists are a must-know in Python โ like your best buddy that holds your stuff while you code away.