DEV Community

SameerQaisar17
SameerQaisar17

Posted on

Python Lists: The Beginner's Guide

📌 Quick Info

  • Topic: Lists in Python
  • Target Audience: Beginners who know functions and loops
  • Goal: Understand how to store and work with multiple items

1. Introduction

"I used to store one value in a variable. But what if I have 10 names? Or 100 items? That’s where lists come in—and now I use them everywhere."

2. The Problem (Without Lists)

name1 = "Ali"
name2 = "Sara"
name3 = "Ahmed"
name4 = "John"
name5 = "Emma"

print(name1)
print(name2)
print(name3)
Enter fullscreen mode Exit fullscreen mode

Problem: Imagine doing this with 50 names. Messy and unmanageable

3. The Solution (With Lists)

names = ["Ali", "Sara", "Ahmed", "John", "Emma"]

for name in names:
    print(name)
Enter fullscreen mode Exit fullscreen mode

Output:

Ali
Sara
Ahmed
John
Emma
Enter fullscreen mode Exit fullscreen mode

4. How It Works (Line by Line)

Line 1: names= [” Ali”,”Sara”,… ]- creates a list with 5 items.

Line 3: for name in names: - Loops through each item.

Line 4: print(name) - prints each one.

That’s it. One List, one loop- cleaner than 5 variables.

5. Common List Operators

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

fruits.append("mango")

fruits.remove("banana")

print(fruits[0]) 

print(len(fruits))
Enter fullscreen mode Exit fullscreen mode

6. Real Examples (My Practise)

scores = [85, 92, 78, 95, 88]

total = 0
for score in scores:
    total += score

average = total / len(scores)
print(f"Average: {average}")
Enter fullscreen mode Exit fullscreen mode

Output:

Average: 87.6
Enter fullscreen mode Exit fullscreen mode

7. What I Learned

  • Lists store multiple items in one variable.
  • Indexes start at 0, not 1.
  • append() adds an item.
  • remove() deletes an item.
  • len() tells you how many items.
  • Loops+ Lists are a perfect match.

8. Conclusion

Lists seemed simple at first. But once I combined them with loops and functions, my code got way more powerful. Now I can’t imagine.

Top comments (0)