Back to all posts

List,Tuple,Set and Dictionary in Python

🔹 List (सूची) 📌 Definition : List ek aisi collection hai jo ordered (क्रमबद्ध) aur mutable (बदलने योग्य) होती है. Ismein duplicate values allowed hain. m…

🔹 List (सूची)

📌 Definition: List ek aisi collection hai jo ordered (क्रमबद्ध) aur mutable (बदलने योग्य) होती है. Ismein duplicate values allowed hain.

Plain Text
my_list = 

Accessing elements:

Bash
print(my_list)    # 1
print(my_list)   # XYZ (last element)

✏️ Update (element बदलना):

Bash
my_list = 5
print(my_list)  # 

Append (अंत में जोड़ना):

Bash
my_list.append('ABC')
print(my_list)  # 

🧩 Insert (बीच में जोड़ना):

Bash
my_list.insert(0, 'New')
print(my_list)  # 

Delete by Index:

Python
del my_list
print(my_list)  # 

Remove by Value:

Bash
my_list.remove(2)
print(my_list)  # 

📤 Pop (निकालना और value return करना):

Bash
fruits = 
print(fruits.pop())      # cherry
print(fruits)            # 

print(fruits.pop(0))     # apple
print(fruits)            # 

🔢 Sorting:

Python
nums = 
nums.sort()
print(nums)              # 

print(sorted(nums, reverse=True))  # 

🧬 Slicing (Part of list):

Bash
players = 
print(players)  # 

🧪 Copying list (deep copy vs shallow):

PHP
my_foods = 
friend_foods = my_foods  # Same reference

my_foods.append('cake')
print(friend_foods)  # 

# Correct way to copy
friend_foods = my_foods
Bash
squared = 
print(squared) # Output: 

🔸 Tuple (अपरिवर्तनीय सूची)

📌 Tuple ek aisi list hai jo immutable(No CUD) होती है.

SQL
my_tuple = (10, 20, 30)
single_element = (5,) # Tuple with one element (comma required)

🔸 Set (सेट)

📌 Set ek unordered, unchangeable (element को update नहीं कर सकते), unindexed collection hoti hai. Duplicate elements allowed नहीं हैं.

unindexed hota hai isliye isse update nahi kar sakte hai , add and remove kar sakte hai ,

Bash
my_set = {"apple", "banana", "apple", "cherry"}
print(my_set)  # {'banana', 'apple', 'cherry'}

Bool True aur int 1 ko same maana jata hai:

Python
s = {"apple", True, 1, 2}
print(s)  # {'apple', True, 2}
PHP
my_set = {1, 2, 3, 4}
my_set.add(5)
# {1, 2, 3, 4, 5}
my_set.remove(2)
# {1, 3, 4, 5}
my_set.discard(10) # No error if element not found
my_set.pop()
# Removes random element

🔸 Dictionary (शब्दकोश)

📌 Dictionary key-value pairs का collection है. Ye ordered (Python 3.7+), mutable, aur duplicate keys allow नहीं karta.

Bash
person = {
    "name": "John",
    "age": 30,
    "occupation": "Engineer"
}

Accessing values:

Bash
print(person)         # John
print(person.get("age"))      # 30
print(person.get("salary", "N/A"))  # N/A

🔧 Methods:

Bash
print(person.keys())     # dict_keys()
print(person.values())   # dict_values()
print(person.items())    # dict_items()

🔁 Loop through dictionary:

Python
for key, value in person.items():
    print(f"{key} => {value}")

🔚 Summary in One Line:

TypeOrderedMutableDuplicatesIndexingSyntax
List
Tuple( )
Set{ }
Dictionary✅ (values)✅ (keys){key:val}

Keep building your data skillset

Explore more SQL, Python, analytics, and engineering tutorials.