Working with Lists, Tuples, and Dictionaries in Python

Learning Python for Beginners — Part 4

By Data Impala

In the last chapter of our journey through Python, we explored how to use if/else conditions and loops to make decisions and repeat actions. 

Now it’s time to learn how to organize and store multiple pieces of information in one place.

Think of real life:

You have a grocery list (a list of items).

A meal combo at a restaurant usually has a fixed set of items (tuple).

A contact in your phone has a name and a number (dictionary).

Python gives you powerful tools to handle all of this in code using:

  • Lists
  • Tuples
  • Dictionaries

Let’s walk through each one — with examples and simple explanations.

Lists: When You Have a Collection That Can Change

A list is a flexible container where you can keep many values in one place. 

The values are kept in order and can be changed any time.

Creating a List

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

This list holds 3 fruits.

Accessing Items in a List

Each item has a position (index). Python starts counting from 0.

print(fruits[0])  # Output: apple

print(fruits[2])  # Output: orange

Changing a Value

fruits[1] = "mango"

print(fruits) 

You’ll get the following output:

['apple', 'mango', 'orange']

Adding Items

Using .append method adds new items to the end of the list.

fruits.append("grape") 

Output:

['apple', 'mango', 'orange']

Inserting at a Position

fruits.insert(1, "kiwi")

This adds ‘kiwi’ at position 1

Removing Items

fruits.remove("apple")  

It removes ‘apple’ from the list.

Checking Length

You can check the length of a list by using len() method.

print(len(fruits))

 This checks the number of items in the list ‘fruits’ and returns the answer.

Tuples: For Fixed Collections

A tuple is like a list, but once created, it cannot be changed. 

Think of it like a set meal: you can’t change what’s inside.

Creating a Tuple

Creating a tuple is quite similar to a list. But you’ll need to use parentheses instead of square brackets.

meal = ("burger", "fries", "drink")

Accessing Items

You can access the items of a tuple the same way as a list:

print(meal[0])  

Output:

burger

Why Use Tuples?

Tuple has a number of advantages over lists. Its:

  • Safer: You can’t accidentally change the values.
  • Faster: Tuples take less memory.
  • Ideal for things that don’t need to change (e.g., days of the week).

Dictionaries: When You Want to Store Pairs

A dictionary lets you store data in pairs — like name: value.

Think of a contact list: each person’s name is linked to a number.

Creating a Dictionary

We use curly brackets to indicate the creation of a dictionary in Python:

person = {
    "name": "Alice",
    "age": 28,
    "city": "Dhaka"
}

Accessing a Value by Key

print(person["name"])

 This returns the output:

Alice

Adding New Data

You can add new data in the dictionary:

person["email"] = "alice@email.com"

Changing a Value

Here’s how you change any value in a dictionary:

person["age"] = 29

Removing a Key-Value Pair

del person["city"]

It removes the city ‘Dhaka’.

Checking Keys

print(person.keys())

 It shows all the keys.

Looping Through Dictionary

Now, you want to look at each item inside this dictionary — one by one.

This line of code helps you do that:

for key, value in person.items():

    print(key, "=>", value)

What’s Happening?

  • person.items()
    This gives you all the key-value pairs in the dictionary.
  • for key, value in ...
    This means:
    ➤ take each pair
    ➤ name the first part key (like “name”)
    ➤ name the second part value (like “Alice”)
  • print(key, "=>", value)
    This shows the key and value like this:
name => Alice  
age => 28  
city => Dhaka

Real-Life Use Case: Online Shopping Cart

Here’s how these data types might work in a small e-commerce app.

art = ["T-shirt", "Jeans", "Sneakers"]  # List of items
price = (10, 25, 50)                     # Fixed prices (tuple)
user = {
    "name": "Rafi",
    "location": "Chattogram",
    "cart_items": cart
}                                        # Dictionary with user info

You’re storing:

  • Items in a list
  • Prices in a tuple
  • Everything else in a dictionary

This is how most real-world programs manage information.

Summary Table

Data TypeCan Change?Ordered?Use When
List✅ Yes✅ YesGroup of items you might update.
Tuple❌ No✅ YesFixed group, like months of the year
Dictionary✅ Yes❌ No*Key-value storage like a contact book

*Note: Dictionaries in Python 3.7+ do maintain insertion order.

Mini Quiz: Test Yourself

Try answering these without running them in Python right away. Think first!

1. What will this print?

names = ["Lina", "Sara", "Tina"]
print(names[1])

2. How would you add “Dhaka” to this list?

cities = ["Barisal", "Sylhet"]

3. What’s wrong with this code?

tools = ("hammer", "screwdriver")
tools[0] = "drill"

4. What will this show?

pet = {
    "name": "Casper",
    "type": "Cat"
}
print(pet["type"])

5. Create a dictionary to store the name and favorite programming language of 2 friends.

Closing Thought

Lists, tuples, and dictionaries are like the building blocks of Python. 

Whether you’re analyzing survey responses, managing books in a library, or tracking users in an app — these data types will come up again and again.

In the next chapter of this series, we’ll look at how to create your own functions — reusable blocks of code that make your programs tidy, readable, and more powerful.

Written by Ahnaf Chowdhury

Series: Learning Python for Beginners — Part 4

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top