Compound data types in Python

In Python, compound data types allow you to group and manage multiple values under one variable. This makes your code cleaner, more readable, and highly efficient—especially when working with collections of data like names, scores, or database records.

In this guide, you’ll learn:

  • The 4 main compound types in Python
  • Key differences between them
  • Real-world use cases and interview questions
  • How to choose the right type for your task


What Are Data Types in Python?

Python has two broad categories of data types:

CategoryExamples
Simple (Primitive)int, float, str, bool
Compound (Composite)list, tuple, set, dict

Simple types store single values, while compound types store multiple values in one variable.

1. Python List – Ordered, Mutable & Versatile

🔹 Syntax:

pythonCopyEditmy_list = [10, 20, 30, 40]

Key Features:

  • Ordered
  • Allows duplicates
  • Mutable (can be changed after creation)

Use Cases:

  • Storing scores: scores = [89, 92, 75]
  • Shopping cart items
  • Iterative operations (loops)

Interview Insight:

“What is the difference between a list and a tuple?”
→ Lists are mutable, while tuples are immutable.

2. Python Tuple – Ordered but Immutable

🔹 Syntax:

pythonCopyEditmy_tuple = (10, 20, 30)

Key Features:

  • Ordered
  • Immutable
  • Faster than lists for read-only data

Use Cases:

  • Storing coordinates: (10.5, 22.3)
  • Returning multiple values from a function

Best Practice:

Use tuples when data should not be changed.

Python Set – Unordered and Unique

🔹 Syntax:

pythonCopyEditmy_set = {1, 2, 3}

Key Features:

  • Unordered
  • No duplicate values
  • Mutable but only stores hashable items

Use Cases:

  • Removing duplicates from a list
  • Performing set operations (union, intersection)
pythonCopyEditset1 = {1, 2, 3}
set2 = {3, 4, 5}
print(set1 & set2) # Intersection


Python Dictionary – Key-Value Pair Storage
🔹 Syntax:
python
Copy
Edit
my_dict = {'name': 'John', 'age': 30}
✅ Key Features:
Unordered (but ordered since Python 3.7)

Fast lookup using keys

Mutable

🔍 Use Cases:
JSON data structures

API responses

Mapping fields to values


Common Interview Questions



What is the difference between list and tuple in Python?
Lists are mutable; tuples are immutable and faster for iteration.

How does a dictionary differ from a set?
Dictionaries store key-value pairs; sets store only unique values.

Can you store a list inside a dictionary?
Yes, dictionaries can store compound types as values.