Skip to main content

Python Lists 🚀

Python Lists & Methods is a core Python concept covering learn how to use Python Lists to store and manage collections of data. Master indexing, slicing, and common list methods like append, pop, and sort. This topic is essential for academic learning, board exam preparation, and developing optimized real-world code.

Mentor's Note: A list is like a multi-purpose container. It's the most flexible and commonly used way to store data in Python! 💡


🌟 The Scenario: The Shopping Bag 🛍️

Imagine you are going to the market with a reusable shopping bag.

  • The Logic:
    • You can put many items inside (Apple, Milk, Bread). 📦
    • The items are ordered (the Apple is at the bottom, the Bread is on top). 🔢
    • You can change the items (swap the Milk for Juice). 🔄
    • You can have duplicates (you can buy two Apples). 🍎🍎
  • The Result: A flexible collection of items that you can manage easily. ✅

📖 Concept Explanation

1. What is a List?

A list is a collection which is ordered and changeable. It allows duplicate members.

2. Creating & Accessing

Lists are written with square brackets [].

fruits = ["Apple", "Banana", "Cherry"]
print(fruits[0]) # Output: Apple (Index starts at 0!)

3. Changeable (Mutable)

Unlike Strings, you can change a specific item in a list:

fruits[1] = "Mango" # Banana is now replaced! 🥭

🎨 Visual Logic: List Anatomy


💻 Implementation: The List Lab

# 🛒 Scenario: Managing a Task List
# 🚀 Action: Adding, removing, and sorting tasks

tasks = ["Code", "Eat", "Sleep"]

# 1. Add a new task ➕
tasks.append("Review")

# 2. Insert at a specific spot 📍
tasks.insert(1, "Fix Bug")

# 3. Remove the last completed task ❌
done = tasks.pop() # Removes "Review"

# 4. Sort alphabetically 🔡
tasks.sort()

print(f"Remaining tasks: {tasks}")
# 🛍️ Outcome: ["Code", "Eat", "Fix Bug", "Sleep"] (sorted)

📊 Sample Dry Run (Methods)

Initial List: L = [10, 20]

InstructionList StateDescription
L.append(30)[10, 20, 30]30 added to end 📥
L.pop(0)[20, 30]Item at index 0 (10) removed 📤
L.extend([40, 50])[20, 30, 40, 50]Multiple items added 🛍️

📉 Complexity Analysis

  • Accessing by Index: $O(1)$ - Instant! ⚡
  • Adding to End (append): $O(1)$ - Very fast.
  • Inserting/Deleting at Start: $O(n)$ - Slow (Python has to shift all other items).

🎯 Practice Lab 🧪

Task: The Guest List

Task: Create a list of 3 friends. Add a new friend to the beginning of the list using insert(). Then, print the total number of guests using len(). Hint: insert(0, "Name"). 💡



← Back: Control Flow | Next: Tuples →


📍 Visit Us

🏫 VD Computer Tuition Surat

VD Computer Tuition
📍 Address
2/66 Faram Street, Rustompura
Surat395002, Gujarat, India
📞 Phone / WhatsApp
+91 84604 41384
🌐 Website

Computer Classes & Tuition — Areas We Serve in Surat

AdajanAlthanAmroliAthwaAthwalinesBhagalBhatarBhestanCanal RoadChowkCitylightDumasGaurav PathGhod Dod RoadHaziraJahangirpuraKamrejKapodraKatargamLimbayatMagdallaMajura GateMota VarachhaNanpuraNew CitylightOlpadPalPandesaraParle PointPiplodPunaRanderRing RoadRustampuraSachinSalabatpuraSarthanaSosyo CircleUdhnaVarachhaVed RoadVesuVIP Road
📞 Call Sir💬 WhatsApp Sir