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.


<
Previous Post
๐Ÿง  Python Control Statements: Making Decisions in Python; The Power of Control Statements
>
Next Post
๐Ÿงพ How to Get a Remita Payment Link After Generating RRR