📘 Lesson · Lesson 80
List vs Tuple vs Set vs Dict
List vs Tuple vs Set vs Dict
The Four Collections
💡 At a Glance
Python has four built-in collection types. Knowing when to use each is a very common interview question.
Comparison Table
| Type | Ordered | Changeable | Duplicates | Syntax |
|---|---|---|---|---|
| List | Yes | Yes | Yes | [1, 2, 3] |
| Tuple | Yes | No | Yes | (1, 2, 3) |
| Set | No | Yes | No | {1, 2, 3} |
| Dict | Yes (3.7+) | Yes | No keys | {"a": 1} |
Example
Python
my_list = [1, 2, 2, 3] # allows duplicates
my_tuple = (1, 2, 3) # cannot change
my_set = {1, 2, 2, 3} # removes duplicate -> {1,2,3}
my_dict = {"name": "Aman"} # key-value pairs
print(my_set){1, 2, 3}
Summary
- List = ordered, changeable, allows duplicates.
- Tuple = ordered, fixed. Set = unordered, unique. Dict = key-value pairs.
चार Collections
💡 एक नज़र में
Python में चार built-in collection types हैं। कब किसे use करें यह बहुत common interview question है।
तुलना Table
| Type | Ordered | Changeable | Duplicates | Syntax |
|---|---|---|---|---|
| List | हाँ | हाँ | हाँ | [1, 2, 3] |
| Tuple | हाँ | नहीं | हाँ | (1, 2, 3) |
| Set | नहीं | हाँ | नहीं | {1, 2, 3} |
| Dict | हाँ (3.7+) | हाँ | keys नहीं | {"a": 1} |
Example
Python
my_list = [1, 2, 2, 3] # duplicates allowed
my_tuple = (1, 2, 3) # बदल नहीं सकते
my_set = {1, 2, 2, 3} # duplicate हटाता है -> {1,2,3}
my_dict = {"name": "Aman"} # key-value pairs
print(my_set){1, 2, 3}
सारांश
- List = ordered, changeable, duplicates allowed।
- Tuple = ordered, fixed। Set = unordered, unique। Dict = key-value।