Master Python Programming From Scratch

Clear, interactive, and structured coding lessons designed for absolute beginners.

Data Structures

Learn how Python stores, organizes and manages collections of data using Lists, Tuples, Sets and Dictionaries.

What are Data Structures?

Data structures are ways of organizing and storing data so that a program can access, update and process it efficiently.

Python provides several built-in data structures that are very easy to use.

  • List
  • Tuple
  • Set
  • Dictionary

Choosing the correct data structure makes your Python programs easier to write, understand and maintain.

Python Data Structures at a Glance

Data Structure Ordered Mutable Duplicates Example
List Yes Yes Allowed [10, 20, 30]
Tuple Yes No Allowed (10, 20, 30)
Set No guaranteed order Yes Not allowed {10, 20, 30}
Dictionary Insertion order Yes Keys unique {"name": "Rahul"}

1. List

A List is an ordered and mutable collection. It can store multiple values in a single variable.

Creating a List

courses = ["Python", "Java", ".NET", "Data Science"]

print(courses)

Output:

['Python', 'Java', '.NET', 'Data Science']

Accessing List Elements

courses = ["Python", "Java", ".NET"]

print(courses[0])
print(courses[1])
print(courses[2])

Output:

Python
Java
.NET

Adding Elements

courses = ["Python", "Java"]

courses.append(".NET")

print(courses)

Output:

['Python', 'Java', '.NET']

Updating Elements

courses = ["Python", "Java", ".NET"]

courses[1] = "MERN"

print(courses)

Output:

['Python', 'MERN', '.NET']

Removing Elements

courses = ["Python", "Java", ".NET"]

courses.remove("Java")

print(courses)

Output:

['Python', '.NET']

Common List Methods

Method Purpose
append() Adds an item at the end
insert() Adds an item at a specific position
remove() Removes a specific item
pop() Removes an item using index
sort() Sorts the list
reverse() Reverses the list
len() Returns number of elements

2. Tuple

A Tuple is an ordered collection that cannot be changed after creation.

Tuples are useful when data should remain unchanged.

student = ("Rahul", 22, "Python")

print(student)

Output:

('Rahul', 22, 'Python')

Accessing Tuple Elements

student = ("Rahul", 22, "Python")

print(student[0])
print(student[2])

Output:

Rahul
Python

Tuple Cannot Be Modified

student = ("Rahul", 22, "Python")

student[1] = 23

This produces a TypeError because tuples are immutable.

3. Set

A Set is a collection used when unique values are required.

Duplicate values are automatically removed.

courses = {"Python", "Java", "Python", ".NET"}

print(courses)

Example Output:

{'Python', 'Java', '.NET'}

Adding an Item

courses = {"Python", "Java"}

courses.add(".NET")

print(courses)

Removing an Item

courses = {"Python", "Java", ".NET"}

courses.remove("Java")

print(courses)

Set Operations

python_students = {"Rahul", "Amit", "Sneha"}

java_students = {"Amit", "Sneha", "Priya"}

print(python_students | java_students)

print(python_students & java_students)

print(python_students - java_students)

Operations:

  • | → Union
  • & → Intersection
  • - → Difference

4. Dictionary

A Dictionary stores data in key-value pairs.

student = {
    "name": "Rahul",
    "age": 22,
    "course": "Python"
}

print(student)

Output:

{'name': 'Rahul', 'age': 22, 'course': 'Python'}

Accessing Values

student = {
    "name": "Rahul",
    "age": 22,
    "course": "Python"
}

print(student["name"])
print(student["course"])

Output:

Rahul
Python

Adding a New Key

student["city"] = "Pune"

print(student)

Updating a Value

student["age"] = 23

print(student["age"])

Output:

23

Removing a Key

student.pop("city")

print(student)

Common Dictionary Methods

Method Purpose
keys() Returns dictionary keys
values() Returns dictionary values
items() Returns key-value pairs
get() Safely gets a value
pop() Removes a key-value pair
update() Updates dictionary values
student = {
    "name": "Rahul",
    "course": "Python",
    "city": "Pune"
}

print(student.keys())
print(student.values())
print(student.items())

Nested Data Structures

Python data structures can contain other data structures.

students = [
    {
        "name": "Rahul",
        "course": "Python"
    },
    {
        "name": "Sneha",
        "course": ".NET"
    }
]

print(students[0]["name"])
print(students[1]["course"])

Output:

Rahul
.NET

This type of structure is very common when working with JSON data and REST APIs.

CIIT Example 📩🤓

Suppose CIIT needs to store student details. A dictionary can represent one student and a list can store multiple students.

students = [
    {
        "name": "Rahul",
        "course": "Python",
        "fees": 35000
    },
    {
        "name": "Sneha",
        "course": ".NET",
        "fees": 35000
    },
    {
        "name": "Amit",
        "course": "Java",
        "fees": 30000
    }
]

for student in students:

    print(student["name"])
    print(student["course"])
    print(student["fees"])
    print("----------------")

Output:

Rahul
Python
35000
----------------
Sneha
.NET
35000
----------------
Amit
Java
30000
----------------

Which Data Structure Should You Use?

Requirement Recommended
Collection can change List
Data should not change Tuple
Only unique values required Set
Key-value data required Dictionary

Common Mistakes

  1. Trying to modify a Tuple.
  2. Assuming Set elements always have a fixed display order.
  3. Accessing a Dictionary key that does not exist without using get().
  4. Confusing List indexes with Dictionary keys.
  5. Choosing a data structure without considering whether the data needs to change.

Interview Points

  • What is the difference between List and Tuple?
  • Why are Sets used?
  • What is a Dictionary?
  • Can a List contain different data types?
  • How do you remove duplicate values from a collection?
  • What is the difference between List, Set and Dictionary?

CIIT Learning Point

Strong knowledge of Python data structures is essential for writing clean programs, solving coding problems and working with real-world APIs, databases and application data.

CIIT Practice Tasks

  1. Create a list of 10 course names and display them using a loop.
  2. Create a Tuple containing student name, age and course.
  3. Create a Set containing duplicate course names and observe the result.
  4. Create a Dictionary for a student with name, email, course and fees.
  5. Create a list of dictionaries containing five CIIT students.
  6. Find students enrolled in the Python course from the list of dictionaries.

Summary :

Python provides powerful built-in data structures such as Lists, Tuples, Sets and Dictionaries. Lists are ordered and mutable, Tuples are ordered and immutable, Sets are useful for unique values, and Dictionaries store data using key-value pairs. Understanding these structures is essential for writing efficient Python programs and working with real-world application data.