Python Cheat Sheet Part 04 - Data Structures (Lists, Tuples, Sets, Dictionaries)

Welcome to Cyber Supto! I'm Supto. In this Python Cheat Sheet series, we are learning Python step-by-step with quick explanations and practical examples that developers can quickly reference while coding.

In previous parts we learned Python basics, control flow, and functions. In this part we will cover one of the most important topics in Python: Data Structures. These allow developers to store, organize, and manipulate data efficiently.

What are Data Structures in Python?

Python provides several built-in data structures used in almost every program.

  • Lists
  • Tuples
  • Sets
  • Dictionaries

Each structure is designed for different use cases.

Python Lists

A list is an ordered and mutable collection of items.

languages = ["Python", "JavaScript", "C++"]
print(languages)

Lists allow duplicate values and can store multiple data types.

Accessing List Items

languages = ["Python", "JavaScript", "C++"]

print(languages[0])
print(languages[1])

Negative Indexing

languages = ["Python", "JavaScript", "C++"]

print(languages[-1])

This accesses elements from the end.

Adding Items to a List

languages = ["Python", "JavaScript"]

languages.append("Go")
languages.insert(1, "Java")

Removing List Items

languages = ["Python", "JavaScript", "C++"]

languages.remove("C++")
languages.pop()

Looping Through a List

languages = ["Python", "JavaScript", "C++"]

for lang in languages:
    print(lang)

Python Tuples

A tuple is similar to a list but immutable, meaning its values cannot be changed after creation.

colors = ("red", "green", "blue")
print(colors)

Accessing Tuple Items

colors = ("red", "green", "blue")

print(colors[0])

Tuples are often used for fixed data.

Python Sets

A set is an unordered collection of unique elements.

numbers = {1, 2, 3, 4}
print(numbers)

Sets automatically remove duplicate values.

Adding Items to a Set

numbers = {1,2,3}

numbers.add(4)

Set Operations

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

print(a.union(b))
print(a.intersection(b))

Sets are useful for mathematical operations.

Python Dictionaries

A dictionary stores data in key-value pairs.

user = {
    "name": "Supto",
    "age": 20,
    "role": "Developer"
}

print(user)

Accessing Dictionary Values

user = {
    "name": "Supto",
    "age": 20
}

print(user["name"])

Adding or Updating Values

user = {"name": "Supto"}

user["age"] = 20
user["role"] = "Developer"

Loop Through Dictionary

user = {"name":"Supto","age":20}

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

Common Dictionary Methods

  • keys()
  • values()
  • items()
  • get()
  • update()

Choosing the Right Data Structure

  • Use lists for ordered, changeable collections
  • Use tuples for fixed data
  • Use sets for unique values
  • Use dictionaries for key-value data

Quick Tips

  • Lists are the most commonly used Python data structure
  • Dictionaries are ideal for structured data
  • Sets are perfect for removing duplicates
  • Tuples are faster and safer for fixed values
  • Choose data structures based on the problem you are solving

Other Parts of This Python Cheat Sheet

This Python Cheat Sheet series on Cyber Supto is designed to provide developers and learners with quick references for writing Python code efficiently.

Thanks for reading,
Cyber Supto.