๐ Python Lists: Vectors of Indian Classrooms ๐ฎ๐ณ๐

Welcome to the most chaotic yet lovable data structure in Python โ lists! But wait โ to make this article 200% more relatable, weโre going desi with a full-on Indian classroom vibe. If youโve ever stood in a roll call line wondering, โWhy is Prashant always first?โ, this blog is for you. ๐
Whether you're a Python noob or someone who still writes print
with capital P (guilty in 1st year ๐
), lists (a.k.a vectors) are your loyal best friends in Python.
๐งบ What is a List?
A list is a collection of items. You can store names, numbers, even mixed things in it.
Example:
# ๐ฆ Vector (List) Basics in Python using Indian Names
# Meet our vector of friends โ think of this like a row of chairs in a classroom!
students = ["Prashant", "Priya", "Sidharth", "Anirudh"]
# ๐งพ Printing the entire list (aka roll call!)
print("Roll Call:", students)
๐ How to Access Items in List?
Each item in a list has a position number, called an index. It starts from 0.
# ๐ฏ Accessing specific students by index (starting from 0)
print("First student:", students[0]) # Output: Prashant
print("Last student:", students[-1]) # Output: Anirudh
โ How to Add an Item to a List?
If someone new joins the class, we can add them using .append()
.
# โ Adding a new student to the list
students.append("Meera")
print("After adding Meera:", students)
๐งน How to Remove an Item from a List?
If someone bunks the class ๐ , we can remove them.
# ๐งน Removing someone who bunked class
students.remove("Sidharth")
print("After removing Sidharth:", students)
๐ How to Change a Name in the List?
If a name is wrong or changed, we can update it.
# ๐ Changing Priya to Simran
students[students.index("Priya")] = "Simran"
print("After changing Priya to Simran:", students)
๐ How to Count How Many Students?
Just use len()
function.
# ๐ Total number of students
print("Total students in class:", len(students))
๐ Loop Through All Names (One by One)
Sometimes, we want to greet or print all names.
# ๐ Saying Hello to everyone
for name in students:
print(f"Hello, {name}!")
๐ชช With Roll Numbers (Using Loop + Index)
We can also print names with numbers.
# ๐ชช Roll numbers with names
for i, name in enumerate(students, start=1):
print(f"Roll No. {i}: {name}")
๐ Check if Someone is Present
You can use in
to check if someone is on the list.
# ๐ Is Anirudh present?
if "Anirudh" in students:
print("Anirudh is present!")
else:
print("Anirudh is absent.")
๐ Birthday Example โ Invite Your Friends
Letโs use list for real-life example: birthday invites.
# ๐ Making a birthday invite list
birthday_invites = ["Prashant", "Meera", "Simran"]
# ๐ Sending invites
for friend in birthday_invites:
print(f"Invitation sent to {friend}!")
Want to change someone on the list?
# ๐ Changing Meera to Tanmay
birthday_invites[birthday_invites.index("Meera")] = "Tanmay"
print("Updated invite list:", birthday_invites)
๐พ Copy the List (Backup Time)
Sometimes, we want to keep a copy before changing anything.
# ๐พ Copy the student list
backup_list = students.copy()
print("Backup list:", backup_list)
๐ Sorting and Reversing the List
We can arrange names A to Z or reverse the order.
# ๐ Sorting names A-Z
students.sort()
print("Sorted students:", students)
# ๐ Reversing the list
students.reverse()
print("Reversed students:", students)
๐ต Common Mistakes (Be Careful!)
- IndexError โ If you try to access a position that doesnโt exist:
# print(students[10]) # โ This will give an error
ValueError โ If you try to remove someone who is not in list:
๐ Wrapping It Up
Python lists arenโt just a feature โ theyโre a vibe. Easy to use, endlessly flexible, and perfect for making your code look smarter than you on a Monday morning.
Remember, a good coder doesnโt just learn syntax โ they connect it with real life. So next time you attend a class, just look around and think: โDamn, this is literally a list.โ
Keep coding and stay spicy ๐ถ๏ธ โ like Maggi in hostel.
๐ Until next time, list lovers!
Subscribe to my newsletter
Read articles from Prashant Kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
