5. Data Structures

Data structures are used to store and organize data efficiently.

Python provides four major built-in data structures:

  • Lists
  • Tuples
  • Sets
  • Dictionaries

Lists in Python

A list is an ordered and mutable collection.

Lists allow:

  • Duplicate values
  • Different data types

Creating Lists

Empty List

numbers = []
print(numbers)

List with Values

fruits = ["Apple", "Banana", "Mango"]

print(fruits)

Mixed Data Types

data = [10, 3.14, "Python", True]

print(data)

Accessing List Elements

fruits = ["Apple", "Banana", "Mango"]

print(fruits[0])
print(fruits[2])

Negative Indexing

print(fruits[-1])

Output:

Mango

List Slicing

numbers = [10, 20, 30, 40, 50]

print(numbers[1:4])

Output:

[20, 30, 40]

Modify List Elements

fruits = ["Apple", "Banana"]

fruits[1] = "Orange"

print(fruits)

List Methods


1. append()

Adds item at end.

numbers = [1, 2, 3]

numbers.append(4)

print(numbers)

2. insert()

Adds item at specific position.

numbers.insert(1, 100)

print(numbers)

3. remove()

Removes specified item.

numbers.remove(100)

print(numbers)

4. pop()

Removes item by index.

numbers.pop(1)

print(numbers)

5. sort()

Sorts list.

values = [5, 2, 8, 1]

values.sort()

print(values)

6. reverse()

Reverses list.

values.reverse()

print(values)

7. len()

Returns length.

print(len(values))

Nested Lists

List inside another list.

matrix = [
[1, 2, 3],
[4, 5, 6]
]

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

Output:

[1, 2, 3]
6

Tuples in Python

A tuple is an ordered and immutable collection.

Cannot be modified after creation.


Creating Tuples

colors = ("Red", "Green", "Blue")

print(colors)

Single Element Tuple

data = (5,)

print(type(data))

Access Tuple Elements

print(colors[0])

Tuple Operations


Concatenation

a = (1, 2)
b = (3, 4)

print(a + b)

Repetition

print(a * 2)

Tuple Packing and Unpacking

Packing

person = ("Aditya", 25, "India")

Unpacking

name, age, country = person

print(name)
print(age)
print(country)

Sets in Python

A set is:

  • Unordered
  • Mutable
  • Unique elements only

Duplicates are automatically removed.


Creating Sets

numbers = {1, 2, 3, 3, 4}

print(numbers)

Output:

{1, 2, 3, 4}

Add Elements

numbers.add(5)

print(numbers)

Remove Elements

numbers.remove(2)

print(numbers)

Set Operations


1. Union

Combines all elements.

a = {1, 2, 3}
b = {3, 4, 5}

print(a | b)

Output:

{1, 2, 3, 4, 5}

2. Intersection

Common elements.

print(a & b)

Output:

{3}

3. Difference

Elements in first set only.

print(a - b)

Output:

{1, 2}

Dictionaries in Python

Dictionary stores data in:

key : value

format.

Dictionary is:

  • Mutable
  • Ordered (Python 3.7+)

Creating Dictionaries

student = {
"name": "Aditya",
"age": 25,
"course": "Python"
}

print(student)

Access Dictionary Values

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

Add or Update Values

student["age"] = 26
student["city"] = "Delhi"

print(student)

Dictionary Methods


1. keys()

Returns all keys.

print(student.keys())

2. values()

Returns all values.

print(student.values())

3. items()

Returns key-value pairs.

print(student.items())

4. get()

Safely gets value.

print(student.get("name"))

5. pop()

Removes item.

student.pop("city")

print(student)

Loop Through Dictionary

for key, value in student.items():
print(key, value)

Nested Dictionaries

Dictionary inside another dictionary.

students = {
"student1": {
"name": "Rahul",
"age": 20
},
"student2": {
"name": "Aman",
"age": 22
}
}

print(students["student1"]["name"])

Output:

Rahul

Comparison of Data Structures

TypeOrderedMutableDuplicates
ListYesYesYes
TupleYesNoYes
SetNoYesNo
DictionaryYesYesKeys No