Introduction to Python Collections

Python Collections

In Python, collections are ways to store multiple values in a single variable. Imagine a box where you can keep many items together – that’s what collections help you do in programming.

Types of Collections

Python has four main types of collections:

  • List – an ordered, changeable collection
  • Tuple – an ordered, unchangeable collection
  • Set – an unordered, unique collection
  • Dictionary – a collection of key-value pairs

1. List – A Changeable Ordered Collection

Lists let you store multiple items in one variable. You can change, add, or remove items.

fruits = ["apple", "banana", "cherry"]
print(fruits)
['apple', 'banana', 'cherry']

Why this output? The list keeps all three fruits in the order they were added. The square brackets [] show it’s a list.

2. Tuple – A Fixed Ordered Collection

Tuples are like lists, but you can’t change them once created.

colors = ("red", "green", "blue")
print(colors)
('red', 'green', 'blue')

Why this output? The items are inside parentheses (), showing it’s a tuple. The order is kept, but the values can’t be changed later.

3. Set – A Unique Unordered Collection

Sets store items without any duplicates and without keeping order.

numbers = {1, 2, 3, 2, 1}
print(numbers)
{1, 2, 3}

Why this output? Sets remove duplicates automatically. The numbers 2 and 1 appeared twice but are shown only once. The order may vary each time you run the code.

4. Dictionary – A Collection of Key-Value Pairs

Dictionaries store data in a key: value format. Each key is unique and helps you find its value.

student = {"name": "Alice", "age": 20}
print(student)
{'name': 'Alice', 'age': 20}

Why this output? The dictionary keeps two key-value pairs. The keys "name" and "age" point to their respective values.

Summary Table – Comparing Python Collections

Type Ordered? Changeable? Duplicates Allowed? Syntax
List Yes Yes Yes [ ]
Tuple Yes No Yes ( )
Set No Yes No { }
Dictionary No (but keys are read in insertion order) Yes No (keys must be unique) { key: value }

When to Use Each Collection?

  • Use a list when you need a collection that can change and has a specific order (e.g., a shopping list).
  • Use a tuple when the collection should never change (e.g., fixed coordinates).
  • Use a set when you only care about unique items (e.g., all unique tags from a blog).
  • Use a dictionary when you need to pair data (e.g., a person's name and age).
Mallikarjuna Mallisetty, author
About the author

Mallikarjuna Mallisetty

General programming

Mallikarjuna shares practical programming tutorials and foundational concepts designed to help developers learn by building and experimenting.

View LinkedIn profile ↗