Data Structures


Data structures in Python are specialized formats for organizing, storing, and manipulating data. They provide efficient ways to access and modify data based on the task. Python’s built-in data structures are:
  1. Lists
  2. Tuples
  3. Sets
  4. Dictionaries
  5. Strings

1. Lists

A list is a mutable, ordered collection of items that can store elements of different data types (e.g., integers, strings, objects). Lists are versatile and widely used for storing and manipulating sequences of data.


Creating a List
my_list = [1, 2, 3, "apple", 5.5]
empty_list = []

#Accessing Elements
print(my_list[0])    # Output: 1 (first element)
print(my_list[-1])   # Output: 5.5 (last element)
print(my_list[1:3])  # Output: [2, 3] (slicing)

#Modifying Elements
my_list[0] = 10
print(my_list)  # Output: [10, 2, 3, "apple", 5.5]

#Adding Elements
my_list.append(6)
my_list.insert(1, "banana")
my_list.extend([7, 8])
print(my_list)  # Output: [10, "banana", 2, 3, "apple", 5.5, 6, 7, 8]

#Removing Elements
my_list.pop()       # Removes 8
my_list.remove(2)   # Removes 2
print(my_list)      # Output: [10, "banana", 3, "apple", 5.5, 6, 7]
my_list.clear()     # Output: []

#Length
print(len(my_list))  # Output: 0 (after clear)

2. Tuples

A tuple is an immutable, ordered collection of items that can store elements of different data types (e.g., integers, strings, objects). Tuples are similar to lists but cannot be modified after creation, making them suitable for fixed data or ensuring data integrity. Defined using parentheses () or just commas, with items separated by commas.


#Creating a Tuple
my_tuple = (1, 2, 3, "apple", 5.5)
empty_tuple = ()
single_item = (42,)  # Note the comma for single-element tuples
no_parens = 1, 2, 3  # Parentheses optional
print(my_tuple)      # Output: (1, 2, 3, "apple", 5.5)
print(no_parens)     # Output: (1, 2, 3)

#Accessing Elements:
print(my_tuple[0])    # Output: 1 (first element)
print(my_tuple[-1])   # Output: 5.5 (last element)
print(my_tuple[1:3])  # Output: (2, 3) (slicing, returns a new tuple)

# my_tuple[0] = 10  # Error: TypeError, tuples are immutable

#Length:
print(len(my_tuple))  # Output: 5


3. Sets

Set is a mutable, unordered collection of unique, immutable elements (e.g., numbers, strings, tuples). Sets are optimized for operations like membership testing, deduplication, and mathematical set operations (union, intersection, etc.). Defined using curly braces {} or the set() function, with elements separated by commas


#Creating a Set:
my_set = {1, 2, 3, "apple"}
empty_set = set()  # Not {}, which creates an empty dict
set_from_list = set([1, 2, 2, 3])
print(my_set)          # Output: {1, 2, 3, 'apple'}
print(set_from_list)   # Output: {1, 2, 3} (duplicates removed)

#Adding Elements:
my_set.add(4)
print(my_set)  # Output: {1, 2, 3, 4, 'apple'}

#Removing Elements:
my_set.remove(3)
print(my_set)       # Output: {1, 2, 4, 'apple'}
my_set.discard(5)   # No error (5 not in set)
popped = my_set.pop()
print(popped, my_set)  # Output: e.g., 1 {2, 4, 'apple'} (random)
my_set.clear()
print(my_set)       # Output: set()

#Length
my_set = {1, 2, 3}
print(len(my_set))  # Output: 3

4. Dictionaries

A dictionary is a mutable, unordered collection of key-value pairs used to store data with unique keys for efficient lookup. Dictionaries are highly flexible and optimized for retrieving values based on their keys. Defined using curly braces {}, with key-value pairs separated by colons key: value and pairs separated by commas


my_dict = {"name": "Alice", "age": 25, "city": "New York"}
my_dict["job"] = "Engineer"
my_dict.pop("age")
print(my_dict)  # Output: {'name': 'Alice', 'city': 'New York', 'job': 'Engineer'}

Creating a Dictionary:
my_dict = {"name": "Alice", "age": 25, "city": "Paris"}
empty_dict = {}
dict_from_list = dict([("a", 1), ("b", 2)])
print(my_dict)  # Output: {'name': 'Alice', 'age': 25, 'city': 'Paris'}

Accessing Values:
print(my_dict["name"])  # Output: Alice
# Using get() (safer, returns None if key missing)
print(my_dict.get("age"))  # Output: 25
print(my_dict.get("country", "Unknown"))  # Output: Unknown (default value)
# Error if key doesn't exist: my_dict["country"] raises KeyError

#Modifying Values:
my_dict["age"] = 26
print(my_dict)  # Output: {'name': 'Alice', 'age': 26, 'city': 'Paris'}


#Adding Key-Value Pairs
my_dict["country"] = "France"
print(my_dict)  # Output: {'name': 'Alice', 'age': 26, 'city': 'Paris', 'country': 'France'}

#Removing Key-Value Pairs
my_dict.pop("city")  # Output: 'Paris'
print(my_dict)       # Output: {'name': 'Alice', 'age': 26, 'country': 'France'}
del my_dict["age"]
print(my_dict)       # Output: {'name': 'Alice', 'country': 'France'}
my_dict.clear()
print(my_dict)       # Output: {}

#Length
my_dict = {"a": 1, "b": 2}
print(len(my_dict))  # Output: 2

5. Strings

A string is an immutable, ordered sequence of characters used to represent text. Strings are versatile and support a wide range of operations for manipulation, formatting, and analysis. Defined using single quotes ', double quotes ", or triple quotes '''/""" for multiline strings.


##Creating a String
single = 'hello'
double = "world"
multiline = '''This is
a multiline
string'''
empty = ""
print(single)       # Output: hello
print(multiline)    # Output: This is\na multiline\nstring

##Accessing Characters:
text = "Python"
print(text[0])     # Output: P
print(text[-1])    # Output: n
print(text[1:4])   # Output: yth (slicing)
# text[0] = 'J'    # Error: strings are immutable

##Length:
print(len(text))   # Output: 6

##Concatenation:
greeting = "Hello, " + "World!"
print(greeting)    # Output: Hello, World!
Feature List Tuple Set Dictionary String
Mutability Mutable Immutable Mutable (elements immutable) Mutable Immutable
Order Ordered Ordered Unordered Unordered (order preserved since 3.7) Ordered
Duplicates Allows duplicates Allows duplicates No duplicates Unique keys Allows duplicate characters
Access Index (list[0]) Index (tuple[0]) No indexing, membership (in) Key (dict["key"]) Index (str[0])
Structure Sequence of items Sequence of items Collection of unique items Key-value pairs Sequence of characters
Use Case Dynamic, ordered data Fixed, lightweight data Deduplication, set operations Key-based lookups Text manipulation