Lists, Tuple, Dictionary and Sets

Reading Time: 4 minutes

๐Ÿ“˜ Data Structures in Python โ€” Complete Study Notes

Python provides several built-in data structures that help organize, store, and manipulate data efficiently. The four core ones are Lists, Tuples, Dictionaries, and Sets. Each serves a unique purpose depending on the situation.


๐Ÿ”น 1. Lists in Python

โœ… Definition

A list is an ordered, mutable (changeable) collection of elements. It allows duplicate values and can store different data types.

๐Ÿ”‘ Key Features

  • Ordered (maintains insertion order)
  • Mutable (can be modified)
  • Allows duplicates
  • Can store mixed data types

๐Ÿ’ป Example Code

# Creating a list
my_list = [10, 20, 30, 40, 50]

# Accessing elements
print(my_list[0])   # First element
print(my_list[-1])  # Last element

# Modifying elements
my_list[1] = 25

# Adding elements
my_list.append(60)

# Removing elements
my_list.remove(30)

print(my_list)

๐Ÿงพ Output

10
50
[10, 25, 40, 50, 60]

๐Ÿ”„ Common List Operations

numbers = [1, 2, 3]

numbers.append(4)       # Add at end
numbers.insert(1, 10)   # Insert at index
numbers.pop()           # Remove last
numbers.sort()          # Sort list

print(numbers)

๐Ÿงพ Output

[1, 2, 3, 10]

๐ŸŽฏ Use Cases

  • Storing sequences (marks, names)
  • Iterating data
  • Dynamic collections

๐Ÿ”น 2. Tuples in Python

โœ… Definition

A tuple is an ordered, immutable collection of elements. Once created, it cannot be changed.

๐Ÿ”‘ Key Features

  • Ordered
  • Immutable (cannot modify)
  • Allows duplicates
  • Faster than lists (in many cases)

๐Ÿ’ป Example Code

# Creating a tuple
my_tuple = (10, 20, 30, 40)

# Accessing elements
print(my_tuple[0])
print(my_tuple[-1])

# Tuple with mixed data types
mixed_tuple = (1, "Hello", 3.5)

print(mixed_tuple)

๐Ÿงพ Output

10
40
(1, 'Hello', 3.5)

โš ๏ธ Immutability Example

my_tuple = (1, 2, 3)

# This will cause an error
# my_tuple[0] = 10

๐Ÿงพ Output

TypeError: 'tuple' object does not support item assignment

๐ŸŽฏ Use Cases

  • Fixed data (coordinates, configuration)
  • Faster read-only operations
  • Returning multiple values from functions

๐Ÿ”น 3. Dictionaries in Python

โœ… Definition

A dictionary stores data in key-value pairs. It is unordered (before Python 3.7), but now maintains insertion order.

๐Ÿ”‘ Key Features

  • Key-value structure
  • Keys must be unique
  • Mutable
  • Fast lookup using keys

๐Ÿ’ป Example Code

# Creating a dictionary
student = {
    "name": "Rahul",
    "age": 21,
    "course": "BCA"
}

# Accessing values
print(student["name"])

# Adding new key-value pair
student["marks"] = 85

# Updating value
student["age"] = 22

# Removing element
del student["course"]

print(student)

๐Ÿงพ Output

Rahul
{'name': 'Rahul', 'age': 22, 'marks': 85}

๐Ÿ”„ Dictionary Methods

data = {"a": 1, "b": 2}

print(data.keys())
print(data.values())
print(data.items())

๐Ÿงพ Output

dict_keys(['a', 'b'])
dict_values([1, 2])
dict_items([('a', 1), ('b', 2)])

๐ŸŽฏ Use Cases

  • Storing structured data (like JSON)
  • Fast searching using keys
  • Representing real-world objects

๐Ÿ”น 4. Sets in Python

โœ… Definition

A set is an unordered collection of unique elements. It does not allow duplicates.

๐Ÿ”‘ Key Features

  • Unordered
  • No duplicate values
  • Mutable
  • Useful for mathematical operations

๐Ÿ’ป Example Code

# Creating a set
my_set = {1, 2, 3, 4}

# Adding elements
my_set.add(5)

# Removing elements
my_set.remove(2)

print(my_set)

๐Ÿงพ Output

{1, 3, 4, 5}

๐Ÿ”„ Set Operations

A = {1, 2, 3}
B = {3, 4, 5}

print(A.union(B))        # Union
print(A.intersection(B)) # Intersection
print(A.difference(B))   # Difference

๐Ÿงพ Output

{1, 2, 3, 4, 5}
{3}
{1, 2}

๐ŸŽฏ Use Cases

  • Removing duplicates
  • Membership testing
  • Mathematical operations

๐Ÿ“Š Comparison Table

FeatureListTupleDictionarySet
OrderedYesYesYes (3.7+)No
MutableYesNoYesYes
DuplicatesAllowedAllowedKeys: NoNot Allowed
IndexingYesYesNoNo

๐Ÿง  Final Summary

  • Lists โ†’ Flexible, most commonly used
  • Tuples โ†’ Fixed data, faster
  • Dictionaries โ†’ Key-value mapping
  • Sets โ†’ Unique elements, mathematical operations

๐Ÿš€ Practice Question

# Write a program to remove duplicates from a list

numbers = [1, 2, 2, 3, 4, 4, 5]

unique_numbers = list(set(numbers))

print(unique_numbers)

๐Ÿงพ Output

[1, 2, 3, 4, 5]

If you want, I can convert these notes into PDF, handwritten-style notes, or exam revision sheets.

๐ŸŽ‰ Great progress! Back to Learning Path โฌ†