Lists And Collections
A variable holds one thing. A list holds many. The first holds the marks of one quiz. The second holds the marks of every quiz this year. Almost every interesting program needs the second. School rosters, family WhatsAp…
There is more than one way to understand this. If you have only been taught one, you have been taught less than you deserve.
There is more than one way to understand this. If you have only been taught one, you have been taught less than you deserve.
A variable holds one thing. A list holds many. The first holds the marks of one quiz. The second holds the marks of every quiz this year. Almost every interesting program needs the second. School rosters, family WhatsApp groups, the items on a kiryana shelf, the messages in your inbox, all are lists. The good news is that lists in Python read almost exactly like a printed list in a register. The brackets, commas, and the ability to count from zero are the only new ideas to swallow.
In Replit, new Python file. Type, classroom = ["Aisha", "Bilal", "Fatima", "Hamza", "Maryam"]. That single line creates a list of five names. Each name lives at a slot, and the slots are numbered. Aisha is at slot zero, Bilal at slot one, Fatima at slot two, and so on. Yes, zero. The world's most famous off by one mistake comes from forgetting that programmers count from zero. To pull a name out, write classroom[0]. To print the third name, classroom[2]. To find out how many names are in the list, len(classroom). Run, print("Class strength is", len(classroom)). Five.
Lists love loops. The two are made for each other. To greet every student in the class, write, for student in classroom, then on the next line, indented, print("Assalam o Alaikum,", student). Run. Five lines of greeting print out, one per student. Notice that you did not say five. You did not say zero through four. You said for each student in this list, do the thing. Tomorrow if a new student joins and the list grows to six, the same loop greets six. Day after, if a student leaves, the loop greets four. The code does not change. The data does. This is a small but life changing idea. From here on, you will always reach for it before writing the same line twice.
A kiryana stock example. stock = ["atta", "daal", "chini", "chai patti", "namak"]. Imagine the chacha at the dukan wants to know if a customer asks for chai patti, is it in stock. The Python expression is, "chai patti" in stock. The answer is true. "machhli", in stock, is false. With one line, the program checks the entire shelf. In an actual point of sale software, the same idea runs against ten thousand items in a few milliseconds. The shape, list plus an in check, is the same whether the shop has five items or fifty thousand. This is one of the deep beauties of programming. Ideas scale. The code that handles five Class 7 students is the code that handles five lakh customers, with very small adjustments.
Adding to and removing from a list. classroom.append("Yusra") puts Yusra at the end. classroom.remove("Bilal") drops Bilal out of the list. Both happen in place, the same list now contains a different set of names. To sort alphabetically, classroom.sort(). To reverse, classroom.reverse(). Each of these is a verb the list knows about itself. You do not write the sorting algorithm. You ask the list to do it. Inside the language, very smart engineers wrote that code so you do not have to. Most of the productivity of a real programmer is not writing what others have already written. It is knowing what is already there to call.
A small reflection to close. Pakistan is a country of crowds. Buses are crowded, weddings are crowded, your khala's drawing room is crowded. The country thinks naturally in lists, even when it does not know it. The shopkeeper who can recall every regular's pasand is running a list in his head. The mosque cleric who reads names of the deceased after juma prayer is reading from a list. When you start to see lists where you used to see only chaos, the world you live in becomes, a little bit, a programmable place. That shift in seeing is what this lesson is really teaching. The code is just the proof.
Estimated time: 12 min