Python Lists, Tuples and Sets: A Beginner's Guide
Store collections in Python — mutable lists, immutable tuples, and unique-value sets — with indexing, methods, and a comprehension teaser.

So far a variable has held one thing — a number, a name, a single piece of text. But real programs deal in groups: a shopping list, the scores in a game, the tags on a post. You don't want twenty variables for twenty fruits. You want one variable that holds all of them. That's what lists, tuples, and sets are for.
Lists
A list is an ordered collection you write inside square brackets, items separated by commas:
fruits = ['apple', 'banana', 'cherry']To pull one item out, you use its position — its index. And here's the thing that trips up everyone: Python counts from zero, not one. So the first item is fruits[0], the second is fruits[1], and so on.
fruits[0] # 'apple'
fruits[1] # 'banana'Counting from the back is even nicer. A negative index walks in from the end, so fruits[-1] is always the last item no matter how long the list is:
fruits[-1] # 'cherry'
fruits[-2] # 'banana'Lists can change after you make them — that's the whole point of them. You add an item with .append() and drop one with .remove():
fruits.append('date') # ['apple', 'banana', 'cherry', 'date']
fruits.remove('banana') # ['apple', 'cherry', 'date']That "can change in place" property has a name: lists are mutable. You modify the same list rather than making a brand-new one. Run the whole thing and watch each line take effect:
Change 'apple' to your own favorite, or .append() a couple more and re-run. The list bends to whatever you do to it.
Tuples
A tuple is a list's stubborn cousin. Same idea — an ordered group of items — but you write it with round brackets, and once it exists you can't change it:
point = (3, 7)
point[0] # 3Try to reassign an item and Python stops you cold:
point[0] = 5 # TypeError: 'tuple' object does not support item assignmentThis is called being immutable — fixed at creation. At first that sounds like a downside (why would you want a collection you can't edit?), but it's exactly what you want when the data shouldn't move. An (x, y) coordinate, a date as (year, month, day), a row from a database — these are fixed records, and a tuple says so out loud. It also protects you: nothing in your code can accidentally shuffle the values around later.
Note
Tuples can't be changed after they're created. Reach for a tuple when the data shouldn't move — coordinates, a fixed record, a pair you'll pass around as one unit. Reach for a list when it will change, like a queue you keep adding to.
Sets
A set uses curly braces and follows one strict rule: every element is unique. Throw duplicates at it and they quietly vanish.
numbers = {1, 2, 2, 3, 3, 3}
print(numbers) # {1, 2, 3}Three 3s went in, one came out. That makes a set the fastest way to dedupe — wrap any list in set() and the repeats disappear. The trade-off is that a set is unordered: there's no set[0], because items don't have positions. You use a set when membership and uniqueness matter and order doesn't.
Sets also do math you'd otherwise write loops for. union (|) combines two sets, intersection (&) keeps only what's in both:
a = {1, 2, 3}
b = {3, 4, 5}
a | b # {1, 2, 3, 4, 5} — everything
a & b # {3} — the overlapThat a & b is genuinely useful: "which users are in both groups?" is one operator, not a nested loop.
Quick check
Which collection guarantees that every element is unique?
Bonus: list comprehensions
Here's a piece of Python that looks like magic until it clicks. Say you want a list of the first five squares. The long way is a loop:
squares = []
for x in range(5):
squares.append(x * x)
# [0, 1, 4, 9, 16]The Python way is one line:
squares = [x * x for x in range(5)]
# [0, 1, 4, 9, 16]That's a list comprehension. Read it left to right almost like a sentence: x * x, for each x in range(5). The range(5) gives you 0, 1, 2, 3, 4; each one gets squared; the results land in a new list. Same output as the loop, a quarter of the typing.
Don't force comprehensions everywhere — a tangled one is harder to read than a plain loop. But for a simple "transform each item into a new list," this is the idiom you'll reach for, and you'll see it constantly in other people's code.
Recap and what's next
Three collections, three jobs. Lists change — add, remove, reorder, index by position. Tuples don't — fixed records you can trust to stay put. Sets dedupe — unique items, no order, with union and intersection built in. Pick the one whose rules match your data, and the rest of your code gets simpler.
You've now got numbers, strings, and collections — the everyday building blocks. Next we add the one that lets you look things up by name instead of position: dictionaries.

Written by
Rhythm Bhiwani
Engineer and relentless builder, happiest reverse-engineering hard problems until they click.
Enjoyed this?
Tap the heart to leave some love.
Be the first to react
Comments
Join the conversation.
Loading comments…


