Data Structures: How They Work & Optimize Memory
A data structures enable rapid access to stored information and seamless updates without performance bottlenecks. Whether handling large datasets or optimizing algorithms, the choice of data structure plays a crucial role in reducing processing time and memory overhead.

Purpose of Data Structures in Python
- Efficient Storage: data structures help manage memory usage effectively, it means they optimize how data is stored, retrieved, and manipulated in a way that minimizes wasted space and maximizes efficiency.
- Faster Data Access: Quick retrieval and updates refer to how efficiently data structures allow access to stored information and modify it without unnecessary delays.
- Supports Algorithms: Many Python functions rely on structured data handling.
Use Cases in Python
- Lists (
list
) – Used for dynamic arrays in applications like social media posts, task lists, or data processing. - Tuples (
tuple
) – Ideal for fixed data storage, such as coordinates or settings that shouldn’t change. - Dictionaries (
dict
) – Powerful key-value storage, often used for databases, configurations, or caching.
1 List (list
)
A list in Python is a mutable , ordered collection that can store element of different data types, lists are commonly used for storing and manipulating Sequence of items.
- Numbers
- Strings
- Other lists
- Or a mix of any data types
1. How to create List
you can create a list using square brackets[]
my_list=[1,2,3,4,5]
print(my_list)
List can Hold different data types.
mixedList=[121,"Hello","Rakesh",3.14,True]
print(mixedList)
2. How to Access element from list
Accessing list element using indexing , index start from 0.
numers=[10,20,30,40,50,60,70]
print(numbers[0])
print(numbers[2])
print(numbers[4])
3. What is slicing
Slicing is a powerful feature in Python that allows you to extract a portion (or sublist) of a list using a specific syntax. It’s useful when you want to access a range of items in a list.
Syntax of List Slicing:
list[start:stop:step]
start
: Index where the slice starts (inclusive).stop
: Index where the slice ends (exclusive).step
: Interval between elements (optional).
numbers=[10,20,30,40,50,60,70]
#print index 1 to 3
print(numbers[1:4])
#print first three elements
print(numbers[:3])
#print from index two onwards
print(numbers[2:])
#print every second element
print(numbers[::2])
4. Modifying Lists
lists re mutable , meaning elements can be changed
#chnging elements
numbers=[10,20,30]
numbers[1]=25
print(numbers)
Adding elements
using append() method adds element at the end .
numbers=[10,20,30]
numbers.append(40)
print(numbers)
Removing elements
using remove() method removes the first occurrence.
numbers=[10,20,30]
numbers.remove(20)
print(numbers)
Key Features of Python Lists
Feature | Description |
---|---|
Ordered | Items have a defined order and position (index starts from 0) |
Changeable | You can add, remove, or update items |
Allows Duplicates | Lists can contain duplicate values |
Heterogeneous | A list can store different data types (e.g., string, int, float, etc.) |
Example
# Creating List
numbers = [10, 20, 30, 40]
mixed = [1, "hello", 3.14, True]
#Accessing list of elements
fruits = ["apple", "banana", "cherry"]
print(fruits[1]) # Output: banana
#Modifying a List
fruits[0] = "mango"
#Common List Methods
fruits.append("orange") # Add item at the end
fruits.remove("banana") # Remove item
fruits.insert(1, "kiwi") # Insert at specific position
len(fruits) # Get length of list
#Looping Through a List
for fruit in fruits:
print(fruit)
#check the list type
fruits = ["apple", "banana", "cherry"]
print(type(fruits)) # Output: <class 'list'>
#check the memory
print(sys.getsizeof(fruits)) # Output: 80+ bytes (varies)
2.Tuple (tuple
)
A tuple used to store multiple items in a single variable, just like a list — but it is immutable.
colors = ("red", "green", "blue")
How to create tuples
Tuples are created using parentheses () , and tuples can contain different data types .
my_tuple=(1,2,3,4,5,"Hello","Rakesh")
print(my_tuple)
How to access tuple
To access elements inside a tuple, you use indexing, which refers to the position of items starting from 0
.
my_tuple = ('apple', 'banana', 'cherry')
print(my_tuple[0]) # Accesses and prints 'apple'
What is slicing in tuples
Tuple slicing is a powerful way to extract a portion of a tuple without changing the original. It works by specifying a range of indices in the format.
tuple[start:stop]
start
is the index where the slice begins (inclusive).stop
is where it ends (exclusive).- Python will include items starting at
start
, up to but not includingstop
.
Example.
numbers=(10,20,30,40,50,60,70)
#print element from index 1 to 3
print(numbers[1:4])
#print first three elements
print(numbers[:3])
#print elements from index2 onwards
print(numbers[2:])
#every second elements
print(numbers[::2])
Key Features of Python Tuples
Feature | Description |
---|---|
Ordered | Items have a defined order (index starts from 0) |
Immutable | You cannot change, add, or remove items after the tuple is created |
Allows Duplicates | Tuples can contain duplicate values |
Heterogeneous | Can hold multiple data types: string, int, float, etc. |
Faster | Slightly faster than lists (due to immutability) |
Why Use Tuples?
- For fixed data sets like coordinates, months, or days of the week
- Tuples can be used as dictionary keys (lists cannot)
Example
#Creating tuples
fruits = ("apple", "banana", "cherry")
numbers = (1, 2, 3, 4)
mixed = (1, "hello", 3.14, False)
# Accessing Tuple Items
print(fruits[1]) # Output: banana
coordinates = (10, 20)
print(type(coordinates)) # Output: <class 'tuple'>
#Check Memory Usage:
print(sys.getsizeof(coordinates)) # Output: 64 bytes
3. Dictionary (dict
)
A dictionary in python is an unordered ,mutable collection of key-value pairs .dictionary in Python is a built-in data type used to store key-value pairs, each key is unique .
- Each item in a dictionary has:
- A key (like a name or label)
- A value (the data linked to that key)
How to create Dictionary
dictionary re defined using curly braces {} with key value pairs .
person = {
"name": "Rakesh",
"age": 38,
"city": "Delhi"
}
Stores Key-Value Pairs
- Think of a dictionary like a real-world dictionary:
- The word is the key
- The definition is the value
- Python dictionaries use a technique called hashing to store keys.
- Keys must be immutable (like strings, numbers, or tuples) and unique.
- You use the key to access the value:
print(person[“name”]) # Output: Rakesh
Dynamic Size — Memory Varies with Entries
- The memory usage of a dictionary depends on:
- The number of key-value pairs
- The size of each key and value
Common Dictionary Methods
pythonCopyEditperson.keys() # Get all keys
person.values() # Get all values
person.items() # Get all key-value pairs
person.get("age") # Safe value access
How to access dictionary
we can access dictionary using keys or using get() method .
person = {
"name": "Rakesh",
"age": 38,
"city": "Delhi"
}
#using keys
print(person["name"])
print(person["age"])
print(person["city"])
#usng get() method
print(get("name"))
print(get("age"))
print(get("city"))