Python List Methods: append(), extend(), insert() Differences Notes in Hindi & English : लिस्ट मेथड्स: append (तत्व जोड़ना), extend (लिस्ट जोड़ना) और insert और 20 महत्वपूर्ण MCQ प्रश्न (Rajasthan IT Exam)

Post a Comment

PGDCA, BCA, B-Tech, MCA, M-Tech, B.Sc computer science राजस्थान सूचना सहायक व कंप्यूटर शिक्षक विशेष नोट्स: इस अध्याय में हम Python List Methods: append(), extend(), insert() Differences (लिस्ट मेथड्स: append (तत्व जोड़ना), extend (लिस्ट जोड़ना) और insert) की संपूर्ण थ्योरी, मुख्य तकनीकी बिंदु, तुलनात्मक तालिका और विगत परीक्षाओं के पैटर्न पर आधारित 20 अति-महत्वपूर्ण बहुविकल्पीय प्रश्न (MCQs) विस्तृत हिंदी व अंग्रेजी व्याख्या सहित पढ़ेंगे।

Overview: Python lists provide append(), extend(), and insert() to add elements in place, all returning None rather than a new list. They differ in whether they add a single object or unpack an iterable, and in their time complexities (amortized O(1) vs O(k) vs O(n)).

अध्याय सार: पायथन लिस्ट में तत्व जोड़ने के लिए append(), extend() और insert() मेथड्स होते हैं, जो लिस्ट को इन-प्लेस बदलते हैं और None लौटाते हैं। ये तीनों सिंगल ऑब्जेक्ट जोड़ने या इटरेबल अनपैक करने तथा अपनी समय जटिलता (O(1), O(k), O(n)) में भिन्न होते हैं।

Python List Methods: append(), extend(), insert() Differences: Detailed Notes & Architecture (लिस्ट मेथड्स: append (तत्व जोड़ना), extend (लिस्ट जोड़ना) और insert)

1. append(x): Single Element at the End (append(x): अंत में एकल तत्व जोड़ना)

[EN] The append(x) method attaches object x as a single item at index len(lst) and returns None. Even if x is another list, tuple, or string, append() does not unpack it; for instance, [1, 2].append([3, 4]) yields [1, 2, [3, 4]] of length 3. Internally, CPython overallocates extra pointer slots geometrically when growing, making append() amortized O(1) time complexity despite occasional buffer reallocations.

[HI] append(x) मेथड किसी भी ऑब्जेक्ट x को लिस्ट के अंत में एक एकल तत्व के रूप में जोड़ता है और None लौटाता है। यदि x स्वयं एक लिस्ट, टपल या स्ट्रिंग हो तब भी append() उसे अनपैक नहीं करता; जैसे [1, 2].append([3, 4]) से [1, 2, [3, 4]] बनता है। CPython आंतरिक रूप से अतिरिक्त मेमोरी स्लॉट्स पहले से आरक्षित रखता है, जिससे append() की औसत समय जटिलता amortized O(1) होती है।

2. extend(iterable): Unpacking Multiple Elements (extend(iterable): इटरेबल से कई तत्व जोड़ना)

[EN] The extend(iterable) method iterates over its argument and appends each yielded item individually to the end of the list in O(k) time, where k is the number of items in iterable. Passing a list, tuple, set, dictionary (adds keys), or string like lst.extend('hi') appends 'h' and 'i' separately. Passing a non-iterable such as an integer raises TypeError: 'int' object is not iterable.

[HI] extend(iterable) मेथड दिए गए इटरेबल पर लूप चलाकर उसके प्रत्येक तत्व को अलग-अलग लिस्ट के अंत में O(k) समय में जोड़ता है। इसमें लिस्ट, टपल, सेट, डिक्शनरी (केवल कीज़ जुड़ती हैं) या स्ट्रिंग भी दी जा सकती है; जैसे lst.extend('hi') से 'h' और 'i' अलग-अलग जुड़ते हैं। यदि कोई नॉन-इटरेबल जैसे पूर्णांक दिया जाए तो TypeError आता है।

3. insert(i, x): Positional Insertion and Element Shifting (insert(i, x): निर्दिष्ट इंडेक्स पर जोड़ना)

[EN] The insert(i, x) method places single element x directly before index i and shifts all subsequent pointers one position to the right, taking O(n) worst-case time (especially when inserting at index 0). Like append(), insert() adds x as a single item without unpacking it and returns None. Out-of-range indices never fail with IndexError: i >= len(lst) appends at the tail, while i <= -len(lst) inserts at index 0.

[HI] insert(i, x) मेथड एकल तत्व x को इंडेक्स i से ठीक पहले रखता है और उसके बाद के सभी तत्वों को दाईं ओर खिसकाता है, जिसमें O(n) समय लगता है (विशेषकर इंडेक्स 0 पर)। यह भी x को बिना अनपैक किए जोड़ता है और None लौटाता है। सीमा से बाहर के इंडेक्स पर IndexError नहीं आता: i >= len(lst) होने पर अंत में और i <= -len(lst) होने पर शुरुआत (इंडेक्स 0) में तत्व जुड़ जाता है।

4. In-Place Return Value (None) and += Equivalence (इन-प्लेस रिटर्न मान (None) और += की समानता)

[EN] A classic Python pitfall is writing a = a.append(5), which overwrites a with None because in-place list mutators follow Python's convention of returning None. Meanwhile, the augmented assignment lst += iterable invokes __iadd__, which behaves identically to lst.extend(iterable) by mutating the existing list object in place. By contrast, lst = lst + [5] requires a list on the right side and constructs an entirely new list object with a new id().

[HI] एक सामान्य त्रुटि a = a.append(5) लिखना है, जिससे a का मान None हो जाता है क्योंकि इन-प्लेस लिस्ट मेथड हमेशा None लौटाते हैं। वहीं lst += iterable आंतरिक रूप से __iadd__ चलाता है, जो lst.extend(iterable) की तरह मूल लिस्ट को इन-प्लेस बढ़ाता है। इसके विपरीत, lst = lst + [5] दाईं ओर केवल लिस्ट स्वीकार करता है और एक नया लिस्ट ऑब्जेक्ट बनाता है।

Syntax, Formula & Technical Example (Python List Methods: append(), extend(), insert() Differences)

a = [1, 2]
a.append([3, 4])      # [1, 2, [3, 4]]
a.extend("hi")        # [1, 2, [3, 4], 'h', 'i']
a.insert(0, 99)       # [99, 1, 2, [3, 4], 'h', 'i']
a.insert(100, 7)      # appends 7 at end
res = a.append(8)     # mutates a, returns None
print(a, res)         # [99, 1, 2, [3, 4], 'h', 'i', 7, 8] None

Python List Methods: append(), extend(), insert() Differences Quick Reference & Comparison Table

Method Call on a = [1, 2]Resulting List StateKey Exam Fact
a.append([3, 4])[1, 2, [3, 4]]Adds [3, 4] as a single nested element; amortized O(1)
a.extend([3, 4])[1, 2, 3, 4]Unpacks iterable elements one by one; O(k) time
a.insert(1, 9)[1, 9, 2]Inserts 9 before index 1 and shifts items right; O(n)
a.extend("hi")[1, 2, 'h', 'i']Iterates over string characters and appends each

Python List Methods: append(), extend(), insert() Differences Top 20 Bilingual MCQs with Answers

Q1. What does x = [1, 2]; y = x.append(3); print(y) output?

(x = [1, 2]; y = x.append(3); print(y) का आउटपुट क्या होगा?)

(A) [3]

(B) [1, 2, 3]

(C) None

(D) 3

Correct Answer: (C)

[EN] append() modifies the list in place and returns None, so y holds None.

[HI] append() लिस्ट को इन-प्लेस बदलता है और None लौटाता है, इसलिए y का मान None होता है।

Q2. What is the length of a after a = [1, 2]; a.append([3, 4, 5])?

(a = [1, 2]; a.append([3, 4, 5]) के बाद a की लंबाई क्या होगी?)

(A) 6

(B) 2

(C) 5

(D) 3

Correct Answer: (D)

[EN] append() adds [3, 4, 5] as a single nested list element at index 2, making len(a) == 3.

[HI] append() पूरी लिस्ट [3, 4, 5] को एक एकल तत्व के रूप में जोड़ता है, जिससे लंबाई 3 होती है।

Q3. What is the output of a = [1, 2]; a.extend('ab'); print(a)?

(a = [1, 2]; a.extend('ab'); print(a) का आउटपुट क्या होगा?)

(A) TypeError

(B) [1, 2, ['a', 'b']]

(C) [1, 2, 'a', 'b']

(D) [1, 2, 'ab']

Correct Answer: (C)

[EN] Since a string is an iterable of single characters, extend('ab') appends 'a' and 'b' separately.

[HI] स्ट्रिंग एक इटरेबल है, इसलिए extend('ab') प्रत्येक अक्षर 'a' और 'b' को अलग-अलग जोड़ता है।

Q4. Which exception occurs on executing [1, 2].extend(3)?

([1, 2].extend(3) चलाने पर कौन-सा एक्सेप्शन उत्पन्न होता है?)

(A) IndexError

(B) AttributeError

(C) ValueError

(D) TypeError

Correct Answer: (D)

[EN] extend() requires an iterable argument; passing an integer 3 raises TypeError: 'int' object is not iterable.

[HI] extend() केवल इटरेबल स्वीकार करता है; पूर्णांक 3 देने पर TypeError आता है।

Q5. What happens when you run a = [10, 20]; a.insert(50, 30); print(a)?

(a = [10, 20]; a.insert(50, 30); print(a) चलाने पर क्या होगा?)

(A) IndexError

(B) [30, 10, 20]

(C) [10, 20, 30]

(D) [10, 20, None, 30]

Correct Answer: (C)

[EN] When the index in insert() exceeds len(a), Python clamps it and places the element at the end without error.

[HI] insert() में इंडेक्स लंबाई से बड़ा होने पर कोई त्रुटि नहीं आती, बल्कि तत्व अंत में जुड़ जाता है।

Q6. What is the worst-case time complexity of inserting at index 0 via lst.insert(0, x)?

(lst.insert(0, x) द्वारा इंडेक्स 0 पर तत्व जोड़ने की वर्स्ट-केस समय जटिलता क्या है?)

(A) O(n^2)

(B) O(1)

(C) O(log n)

(D) O(n)

Correct Answer: (D)

[EN] Inserting at index 0 requires shifting all n existing pointer references one slot to the right, taking O(n) time.

[HI] इंडेक्स 0 पर जोड़ने के लिए पहले से मौजूद सभी n तत्वों को दाईं ओर खिसकाना पड़ता है, जिसमें O(n) समय लगता है।

Q7. What is the amortized time complexity of lst.append(x)?

(lst.append(x) की एमॉर्टाइज्ड (औसत) समय जटिलता क्या है?)

(A) O(n)

(B) O(n log n)

(C) O(1)

(D) O(log n)

Correct Answer: (C)

[EN] CPython overallocates list storage geometrically, so appending at the tail runs in amortized O(1) time.

[HI] CPython पहले से अतिरिक्त मेमोरी आरक्षित रखता है, जिससे अंत में जोड़ना औसत O(1) समय में होता है।

Q8. Which operator behaves like list.extend() by mutating the list in place?

(कौन-सा ऑपरेटर list.extend() की तरह लिस्ट को इन-प्लेस बदलता है?)

(A) +

(B) +=

(C) ==

(D) *

Correct Answer: (B)

[EN] For lists, a += b invokes __iadd__, which iterates over b and extends a in place while keeping id(a) unchanged.

[HI] लिस्ट के लिए += ऑपरेटर __iadd__ चलाता है, जो मूल लिस्ट की id बदले बिना extend() जैसा काम करता है।

Q9. What does a = [1, 2]; a.insert(-10, 99); print(a) output?

(a = [1, 2]; a.insert(-10, 99); print(a) का आउटपुट क्या होगा?)

(A) IndexError

(B) [1, 99, 2]

(C) [99, 1, 2]

(D) [1, 2, 99]

Correct Answer: (C)

[EN] An index smaller than -len(a) clamps to 0 in insert(), placing 99 at the very front of the list.

[HI] insert() में -len(a) से छोटा इंडेक्स 0 पर क्लैम्प होकर तत्व को सबसे आगे जोड़ देता है।

Q10. What is the output of a = [1, 2]; a.extend({'x': 10, 'y': 20}); print(a)?

(a = [1, 2]; a.extend({'x': 10, 'y': 20}); print(a) का आउटपुट क्या होगा?)

(A) [1, 2, 10, 20]

(B) [1, 2, ('x', 10), ('y', 20)]

(C) [1, 2, 'x', 'y']

(D) TypeError

Correct Answer: (C)

[EN] Iterating over a dictionary yields its keys, so extend() appends 'x' and 'y' to the list.

[HI] डिक्शनरी पर इटरेट करने पर उसकी कीज़ मिलती हैं, इसलिए extend() से 'x' और 'y' जुड़ते हैं।

Q11. What happens with a = [1, 2]; a.insert(-1, 5); print(a)?

(a = [1, 2]; a.insert(-1, 5); print(a) चलाने पर क्या आउटपुट मिलेगा?)

(A) [1, 5, 2]

(B) IndexError

(C) [5, 1, 2]

(D) [1, 2, 5]

Correct Answer: (A)

[EN] insert(i, x) inserts before index i; since -1 currently points to 2, 5 is inserted right before 2.

[HI] insert(i, x) इंडेक्स i से पहले जोड़ता है; वर्तमान में -1 पर 2 है, अतः 5 ठीक 2 से पहले जुड़ता है।

Q12. What is the difference between a = a + [3] and a += [3] if b = a?

(यदि b = a हो, तो a = a + [3] और a += [3] में क्या अंतर है?)

(A) a += [3] also updates b, whereas a = a + [3] leaves b unchanged

(B) Both leave b unchanged

(C) a += [3] raises TypeError

(D) Both update b

Correct Answer: (A)

[EN] a += [3] mutates the shared list object in place (visible via alias b), while a = a + [3] binds a to a new list.

[HI] a += [3] साझा ऑब्जेक्ट को इन-प्लेस बदलता है जिससे b भी बदलता है, जबकि a = a + [3] नया ऑब्जेक्ट बनाता है।

Q13. How many arguments does list.insert() require?

(list.insert() को कितने आर्गुमेंट्स की आवश्यकता होती है?)

(A) Variable (0 or more)

(B) 2

(C) 1

(D) 3

Correct Answer: (B)

[EN] insert(index, object) strictly requires two positional arguments: the target index and the object to insert.

[HI] insert(index, object) को ठीक दो आर्गुमेंट्स (इंडेक्स और ऑब्जेक्ट) की आवश्यकता होती है।

Q14. Which code converts a = [1, 2] to [1, 2, (3, 4)]?

(कौन-सा कोड a = [1, 2] को [1, 2, (3, 4)] में बदलेगा?)

(A) a.insert(0, (3, 4))

(B) a.extend((3, 4))

(C) a.append((3, 4))

(D) a += (3, 4)

Correct Answer: (C)

[EN] append((3, 4)) adds the tuple as one object at the end, whereas extend((3, 4)) and += (3, 4) unpack 3 and 4.

[HI] append((3, 4)) पूरे टपल को एक तत्व के रूप में अंत में जोड़ता है, जबकि extend() और += उसे अनपैक कर देते हैं।

Q15. What does a = [1]; a.extend([]); print(a) output?

(a = [1]; a.extend([]); print(a) का आउटपुट क्या होगा?)

(A) [1, []]

(B) ValueError

(C) [1]

(D) []

Correct Answer: (C)

[EN] Extending with an empty iterable iterates zero times and adds no elements, leaving [1] unchanged.

[HI] खाली इटरेबल से extend() करने पर एक भी तत्व नहीं जुड़ता और [1] अपरिवर्तित रहता है।

Q16. Which statement is NOT true about append(), extend(), and insert()?

(append(), extend() और insert() के बारे में कौन-सा कथन सत्य नहीं है?)

(A) insert() returns the new length of the list

(B) extend() fails with TypeError if given an int

(C) All three mutate the list in place

(D) append() takes exactly one argument

Correct Answer: (A)

[EN] None of append(), extend(), or insert() return the new length; all three return None.

[HI] append(), extend() और insert() में से कोई भी नई लंबाई नहीं लौटाता; तीनों None लौटाते हैं।

Q17. Within Python List Methods: append(), extend(), insert() Differences, which method call on a = [1, 2] directly maps to: "[1, 2, [3, 4]]"?

(Python List Methods: append(), extend(), insert() Differences में कौन सा Method Call on a = [1, 2] सीधे "[1, 2, [3, 4]]" से संबंधित है?)

(A) a.extend([3, 4])

(B) a.append([3, 4])

(C) a.insert(1, 9)

(D) a.extend("hi")

Correct Answer: (B)

[EN] In Python List Methods: append(), extend(), insert() Differences, 'a.append([3, 4])' handles '[1, 2, [3, 4]]' (Adds [3, 4] as a single nested element; amortized O(1)).

[HI] Python List Methods: append(), extend(), insert() Differences में 'a.append([3, 4])' का कार्य/विवरण '[1, 2, [3, 4]]' (Adds [3, 4] as a single nested element; amortized O(1)) है।

Q18. Which option accurately describes 'a.extend([3, 4])' in Python List Methods: append(), extend(), insert() Differences?

(Python List Methods: append(), extend(), insert() Differences के संदर्भ में 'a.extend([3, 4])' की सही विशेषता कौन सी है?)

(A) [1, 2, 3, 4]

(B) [1, 9, 2]

(C) [1, 2, 'h', 'i']

(D) [1, 2, [3, 4]]

Correct Answer: (A)

[EN] 'a.extend([3, 4])' provides '[1, 2, 3, 4]' — key point: Unpacks iterable elements one by one; O(k) time.

[HI] 'a.extend([3, 4])' का मुख्य विवरण '[1, 2, 3, 4]' है (Unpacks iterable elements one by one; O(k) time)।

Q19. In Python List Methods: append(), extend(), insert() Differences, identify the correct parameter for the specification: "[1, 9, 2]".

(Python List Methods: append(), extend(), insert() Differences में "[1, 9, 2]" विवरण किस विकल्प के लिए सही है?)

(A) a.extend("hi")

(B) a.append([3, 4])

(C) a.extend([3, 4])

(D) a.insert(1, 9)

Correct Answer: (D)

[EN] 'a.insert(1, 9)' corresponds to '[1, 9, 2]' (Inserts 9 before index 1 and shifts items right; O(n)).

[HI] 'a.insert(1, 9)' का संबंध '[1, 9, 2]' (Inserts 9 before index 1 and shifts items right; O(n)) से है।

Q20. Considering Python List Methods: append(), extend(), insert() Differences, what key behavior defines 'a.extend("hi")'?

(Python List Methods: append(), extend(), insert() Differences में 'a.extend("hi")' की मुख्य भूमिका या मान क्या है?)

(A) [1, 2, [3, 4]]

(B) [1, 2, 3, 4]

(C) [1, 9, 2]

(D) [1, 2, 'h', 'i']

Correct Answer: (D)

[EN] 'a.extend("hi")' represents '[1, 2, 'h', 'i']' (Iterates over string characters and appends each).

[HI] 'a.extend("hi")' का अर्थ/मान '[1, 2, 'h', 'i']' है (Iterates over string characters and appends each)।

Python List Methods: append(), extend(), insert() Differences — Exam FAQs

Q1: Why do append(), extend(), and insert() return None instead of the updated list?

append(), extend() और insert() अपडेटेड लिस्ट के बजाय None क्यों लौटाते हैं?

[EN] Python follows the command-query separation convention for in-place mutators: returning None prevents programmers from mistakenly assuming a new copy was created and discourages accidental chained calls like a.append(1).sort().

[HI] पायथन में इन-प्लेस बदलाव करने वाले मेथड None लौटाते हैं ताकि प्रोग्रामर को स्पष्ट रहे कि मूल ऑब्जेक्ट बदला गया है और कोई नई कॉपी नहीं बनी है।

Q2: When should I use extend() instead of append()?

मुझे append() के बजाय extend() का उपयोग कब करना चाहिए?

[EN] Use append(x) when you want x added as a single item (even if x is a list). Use extend(iterable) when you want to merge all individual elements from another list, tuple, or generator directly into the target list.

[HI] जब किसी वस्तु (चाहे वह लिस्ट ही क्यों न हो) को एक एकल तत्व के रूप में जोड़ना हो तब append() उपयोग करें; और जब दूसरे इटरेबल के सभी तत्वों को एक-एक कर मर्ज करना हो तब extend() चुनें।

Q3: Is lst.insert(0, x) efficient for building a queue in Python?

क्या पायथन में क्यू (Queue) बनाने के लिए lst.insert(0, x) कुशल है?

[EN] No. insert(0, x) shifts every existing element in O(n) time. For frequent insertions and removals from the front, use collections.deque which provides O(1) appendleft() and popleft().

[HI] नहीं। insert(0, x) हर बार सभी तत्वों को खिसकाने में O(n) समय लेता है। शुरुआत से तेज जुड़ाव के लिए collections.deque का appendleft() और popleft() (O(1)) बेहतर है।

Related Posts

  • Loading related posts...

Post a Comment

Search Topic Notes & 20 MCQs (Hindi / English)