Python: Data Structures Basics — Lists, Tuples, and Dictionaries

Introduction

So far, you’ve learned about variables, control flow, and functions. But real-world programs often need to store and organize collections of data. Python provides powerful built-in data structures for this: lists, tuples, and dictionaries. In this tutorial, we’ll explore each one with examples and exercises.

 Previous Tutorial

1: Lists — Ordered, Mutable Collections

A list is an ordered collection that can hold multiple items. Lists are mutable, meaning you can change them after creation.

Code:

fruits = ["apple", "banana", "cherry"]

print(fruits)        # Output: ['apple', 'banana', 'cherry']

print(fruits[0])     # Output: apple

 


 

Adding and Removing Items

Code:

fruits = ["apple", "banana", "cherry"]

print(fruits)                # Output: ['apple', 'banana', 'cherry']

fruits.append("orange")      # Add at the end

print(fruits)      # Output: ['apple', 'banana', 'cherry', 'orange']

fruits.remove("banana")      # Remove by value

print(fruits)                # Output: ['apple', 'cherry', 'orange']

 


 

Iterating Over Lists

Code:

fruits = ["apple", "banana", "cherry"]

for fruit in fruits:

    print(fruit)

 

Note: Lists are perfect for storing sequences of items you want to modify.

 


2: Tuples — Ordered, Immutable Collections

A tuple is similar to a list, but immutable — once created, it cannot be changed.

Code:

coordinates = (10, 20)

print(coordinates[0])   # Output: 10

print(coordinates[1])   # Output: 20


Tuples are useful when you want to ensure data stays constant, like fixed positions or configuration values.

 

Attempting to modify a tuple will cause an error:

coordinates[0] = 15   # Error: 'tuple' object does not support item assignment



3: Dictionaries — Key-Value Pairs

A dictionary stores data as key-value pairs. Think of it like a real dictionary: you look up a word (key) to find its meaning (value).

 

student = {

    "name": "Deepak",

    "age": 30,

    "course": "Python"

}

 

print(student["name"])   # Output: Deepak

print(student["age"])    # Output: 30



Adding and Updating

student = {

    "name": "Deepak",

    "age": 30,

    "course": "Python"

}

print(student)

student["grade"] = "A"       # Add new key-value pair

student["age"] = 31          # Update existing value

print(student)


 

Iterating Over Dictionaries

student = {

    "name": "Deepak",

    "age": 30,

    "course": "Python"

}

for key, value in student.items():

    print(key, ":", value)

 


 Note: Dictionaries are great for structured data like user profiles, settings, or configurations.

 

4: Choosing the Right Data Structure

Lists: Use when you need an ordered collection that can change.

Tuples: Use when you need an ordered collection that must stay constant.

Dictionaries: Use when you need to map keys to values for quick lookups.

 

Mini Exercise

Try this challenge:

·       Create a list of three cities.

·       Convert it into a tuple.

·       Create a dictionary where each city is a key, and the value is the population.

·       Print the dictionary in a readable format.

 

Hint:

populations = {

    "Delhi": 19000000,

    "Mumbai": 20000000,

    "Bengaluru": 12000000

}

for city, population in populations.items():

    print(f"{city} has a population of {population}.")


                    

Conclusion:

In this tutorial, you learned how to:

·       Use lists for ordered, mutable collections

·       Use tuples for ordered, immutable collections

·       Use dictionaries for key-value mappings

 

Data structures are the foundation of organizing information in Python. With these tools, you can manage everything from simple lists of items to complex structured data.

In the next tutorial, we’ll explore loops with data structures — how to process collections efficiently and build small projects like a contact book.