📦 Unit 2 – Strings, Lists, Tuples और Dictionaries · Lesson 22
Nested Lists, Copying और List Programs
Nested lists
List के अंदर दूसरी lists हो सकती हैं। ऐसी nested list tables — rows और columns — के लिए perfect है, जैसे कई students के कई subjects के marks।
| [0] Eng | [1] Maths | [2] CS | |
|---|---|---|---|
| marks[0] | 78 | 85 | 90 |
| marks[1] | 66 | 72 | 81 |
| marks[2] | 90 | 95 | 99 |
Python
marks = [[78, 85, 90], # student 1: Eng, Maths, CS
[66, 72, 81], # student 2
[90, 95, 99]] # student 3
print(marks[1]) # whole row of student 2
print(marks[1][2]) # student 2, CS
print(marks[2][0], len(marks), len(marks[0]))[66, 72, 81]
81
90 3 3
Nested list traverse करना
Python
marks = [[78, 85, 90], [66, 72, 81], [90, 95, 99]]
for row in marks:
for m in row:
print(m, end='\t')
print('| total =', sum(row))78 85 90 | total = 253
66 72 81 | total = 219
90 95 99 | total = 284
Lists copy करना: alias vs copy
Python
a = [1, 2, 3]
alias = a # same list, two names
copy1 = a[:] # slicing makes a copy
copy2 = list(a) # list() makes a copy
copy3 = a.copy() # copy() method
a.append(4)
print('a :', a)
print('alias :', alias)
print('copies:', copy1, copy2, copy3)
print(alias is a, copy1 is a)a : [1, 2, 3, 4]
alias : [1, 2, 3, 4]
copies: [1, 2, 3] [1, 2, 3] [1, 2, 3]
True False
b = a (alias)
- कोई नई list नहीं बनती
- दोनों names एक ही list की ओर
- एक नाम से बदलाव दूसरे को दिखता है
- b is a → True
b = a[:] / list(a) / a.copy()
- नई list बनती है
- Same values वाली दो अलग lists
- एक बदलने से दूसरी पर असर नहीं
- b is a → False
📌 Note
ये shallow copies हैं: nested lists में अंदर की lists अब भी shared रहती हैं। पूरी तरह अलग copy के लिए import copy; copy.deepcopy(L) use करें।
Function को list argument
Python
def increase(prices, percent):
for i in range(len(prices)):
prices[i] = round(prices[i] * (1 + percent / 100), 2)
def new_list(prices):
prices = [0, 0] # re-assigns the LOCAL name only
return prices
p = [100, 250, 80]
increase(p, 10)
print(p)
print(new_list(p), p)[110.0, 275.0, 88.0]
[0, 0] [110.0, 275.0, 88.0]
Linear search
शुरू से एक-एक item check करें जब तक key न मिल जाए।
Python
nums = [34, 7, 89, 21, 56, 7]
key = int(input('Search for: '))
found = False
for i in range(len(nums)):
if nums[i] == key:
print(key, 'found at index', i)
found = True
break
if not found:
print(key, 'not found')Search for: 21
21 found at index 3
Frequency और duplicates हटाना
Python
data = [4, 2, 4, 9, 2, 4, 7]
seen = []
for x in data:
if x not in seen:
seen.append(x)
print(x, 'occurs', data.count(x), 'time(s)')
print('Without duplicates:', seen)4 occurs 3 time(s)
2 occurs 2 time(s)
9 occurs 1 time(s)
7 occurs 1 time(s)
Without duplicates: [4, 2, 9, 7]
सबसे बड़ा और दूसरा सबसे बड़ा (बिना sorting)
Python
nums = [45, 89, 23, 89, 67, 12]
first = second = float('-inf')
for x in nums:
if x > first:
second = first
first = x
elif first > x > second:
second = x
print('Largest:', first, '| Second largest:', second)Largest: 89 | Second largest: 67
float('-inf') "minus infinity" है, हर number से छोटा — शुरुआती value के लिए safe।
Swap और rotate
Python
L = [1, 2, 3, 4, 5, 6]
half = len(L) // 2
print('Swap halves :', L[half:] + L[:half])
print('Rotate left :', L[1:] + L[:1])
print('Rotate right:', L[-1:] + L[:-1])
for i in range(0, len(L) - 1, 2): # swap neighbours
L[i], L[i + 1] = L[i + 1], L[i]
print('Swap pairs :', L)Swap halves : [4, 5, 6, 1, 2, 3]
Rotate left : [2, 3, 4, 5, 6, 1]
Rotate right: [6, 1, 2, 3, 4, 5]
Swap pairs : [2, 1, 4, 3, 6, 5]
Matrix addition और transpose
Python
A = [[1, 2], [3, 4]]
B = [[5, 6], [7, 8]]
S = [[A[i][j] + B[i][j] for j in range(2)] for i in range(2)]
T = [[A[j][i] for j in range(2)] for i in range(2)] # transpose
print('A + B =', S)
print('Transpose of A =', T)A + B = [[6, 8], [10, 12]]
Transpose of A = [[1, 3], [2, 4]]
Menu-driven list program
Python
items = []
while True:
print('1-Add 2-Delete 3-Show 4-Exit')
ch = input('Choice: ')
if ch == '1':
items.append(input('Item: '))
elif ch == '2':
it = input('Item to delete: ')
if it in items:
items.remove(it)
else:
print('Not in list')
elif ch == '3':
print('List:', items)
elif ch == '4':
break1-Add 2-Delete 3-Show 4-Exit
Choice: 1
Item: Pen
1-Add 2-Delete 3-Show 4-Exit
Choice: 1
Item: Book
1-Add 2-Delete 3-Show 4-Exit
Choice: 2
Item to delete: Pencil
Not in list
1-Add 2-Delete 3-Show 4-Exit
Choice: 3
List: ['Pen', 'Book']
1-Add 2-Delete 3-Show 4-Exit
Choice: 4
सारांश और practice
- Nested list = lists की list;
L[i][j]से access। b = aalias है; copy के लिएa[:],list(a),a.copy()।- Functions भेजी गई list बदल सकते हैं।
- Searching, counting, duplicates हटाना, rotate करना practice करें।
✅ Practice
1) 3 × 3 matrix के हर column का जोड़ निकालें। 2) List के सारे zeros अंत में ले जाएँ: [0, 5, 0, 3, 8] → [5, 3, 8, 0, 0]। 3) दो lists के common elements निकालें।
अक्सर पूछे जाने वाले प्रश्न (FAQ)
क्या b = a से list copy होती है?
नहीं। यह उसी list का दूसरा नाम (alias) बनाता है। असली copy के लिए a[:], list(a) या a.copy() use करें।
Nested list का element कैसे access करें?
दो indexes से: L[row][column]। जैसे marks[1][2] दूसरी row का तीसरा item देता है।
Function भेजी गई list बदले तो क्या original बदलती है?
हाँ, अगर function list को उसी जगह बदले (जैसे L[i] = x या L.append)। अगर function parameter को सिर्फ़ नई list से re-assign करे तो original पर असर नहीं होता।
