PGDCA, BCA, B-Tech, MCA, M-Tech, B.Sc computer science राजस्थान सूचना सहायक व कंप्यूटर शिक्षक विशेष नोट्स: इस अध्याय में हम Python List Removal: remove(), pop(), clear() and del Statement (लिस्ट से हटाना: remove() (मान द्वारा), pop() (इंडेक्स) और clear()) की संपूर्ण थ्योरी, मुख्य तकनीकी बिंदु, तुलनात्मक तालिका और विगत परीक्षाओं के पैटर्न पर आधारित 20 अति-महत्वपूर्ण बहुविकल्पीय प्रश्न (MCQs) विस्तृत हिंदी व अंग्रेजी व्याख्या सहित पढ़ेंगे।
Overview: Python removes list elements using remove() by value, pop() by index with return value, clear() to empty in place, and the del statement by index, slice, or variable name. Understanding their return values, time complexities, and ValueError vs IndexError behavior prevents common runtime bugs.
अध्याय सार: पायथन लिस्ट से तत्व हटाने के लिए मान द्वारा remove(), इंडेक्स द्वारा pop() (जो हटाया गया मान लौटाता है), पूरी लिस्ट खाली करने के लिए clear() और del स्टेटमेंट का उपयोग होता है। इनके रिटर्न मान, समय जटिलता और ValueError बनाम IndexError अंतर को समझना परीक्षा के लिए अत्यंत महत्वपूर्ण है।
Python List Removal: remove(), pop(), clear() and del Statement: Detailed Notes & Architecture (लिस्ट से हटाना: remove() (मान द्वारा), pop() (इंडेक्स) और clear())
1. remove(x): Value-Based Removal of First Match (remove(x): मान द्वारा पहले मिलान को हटाना)
[EN] The remove(x) method performs a linear search from index 0 using equality (==), deletes only the first matching occurrence, and shifts trailing elements left in O(n) time. It returns None and does not delete duplicate copies further down the list. If x is not present anywhere in the list, Python raises ValueError: list.remove(x): x not in list.
[HI] remove(x) मेथड इंडेक्स 0 से समानता (==) की रैखिक खोज करके केवल पहले मिलने वाले तत्व को हटाता है और बाद के तत्वों को बाईं ओर खिसकाता है (O(n) समय)। यह None लौटाता है और लिस्ट में मौजूद अन्य डुप्लिकेट मानों को नहीं हटाता। यदि x लिस्ट में न मिले, तो ValueError: list.remove(x): x not in list त्रुटि आती है।
2. pop([i]): Index-Based Removal Returning the Element (pop([i]): इंडेक्स द्वारा हटाकर मान लौटाना)
[EN] The pop(i=-1) method removes the element at index i and returns that removed object to the caller. Calling pop() without arguments removes the last item in O(1) time (making a Python list an efficient LIFO stack), whereas pop(0) or popping from the middle takes O(n) time due to shifting. Calling pop() on an empty list or with an out-of-range index raises IndexError.
[HI] pop(i=-1) मेथड इंडेक्स i पर स्थित तत्व को हटाकर उसका मान लौटाता है। बिना आर्गुमेंट के pop() अंतिम तत्व को O(1) समय में हटाता है (जिससे लिस्ट LIFO स्टैक बन जाती है), जबकि pop(0) में तत्व खिसकाने के कारण O(n) समय लगता है। खाली लिस्ट पर या अमान्य इंडेक्स के साथ pop() चलाने पर IndexError आता है।
3. clear() vs Reassigning an Empty List (clear() और खाली लिस्ट पुनः असाइन करने में अंतर)
[EN] Calling lst.clear() removes all elements from the list object in place (equivalent to del lst[:]) and returns None, freeing element references in O(n) time while preserving id(lst). Consequently, any other variables aliasing the same list immediately see an empty list []. By contrast, executing lst = [] only rebinds the local name lst to a brand-new empty list object, leaving aliases pointing to the old populated list.
[HI] lst.clear() मूल लिस्ट ऑब्जेक्ट की id बदले बिना उसके सभी तत्वों को इन-प्लेस हटा देता है (यह del lst[:] के समान है) और None लौटाता है। इससे इस लिस्ट को संदर्भित करने वाले सभी अन्य वेरिएबल्स भी तुरंत खाली हो जाते हैं। इसके विपरीत, lst = [] केवल वेरिएबल को एक नई खाली लिस्ट से जोड़ता है, जिससे पुराने रेफरेंस प्रभावित नहीं होते।
4. The del Statement: Indices, Slices, and Name Unbinding (del स्टेटमेंट: इंडेक्स, स्लाइस और वेरिएबल हटाना)
[EN] Unlike list methods, del is a Python keyword statement that takes no parentheses and returns nothing. You can write del lst[i] to remove one index (raising IndexError if out of bounds), del lst[start:stop:step] to erase contiguous or strided slices at once, or del lst to unbind the variable name entirely from the namespace (subsequent access raises NameError).
[HI] लिस्ट मेथड्स के विपरीत, del एक पायथन कीवर्ड स्टेटमेंट है जो कोई मान नहीं लौटाता। del lst[i] से किसी एक इंडेक्स का तत्व हटता है (अमान्य होने पर IndexError), del lst[start:stop:step] से पूरा स्लाइस एक साथ हट सकता है, और केवल del lst लिखने से वेरिएबल नाम नेमस्पेस से ही समाप्त हो जाता है (बाद में उपयोग करने पर NameError आता है)।
Syntax, Formula & Technical Example (Python List Removal: remove(), pop(), clear() and del Statement)
a = [10, 20, 30, 20, 40, 50]
a.remove(20) # removes first 20 -> [10, 30, 20, 40, 50]
last = a.pop() # 50; a is [10, 30, 20, 40]
second = a.pop(1) # 30; a is [10, 20, 40]
del a[0:2] # removes 10, 20 -> a is [40]
a.clear() # a is [] (same id)
print(last, second, a) # 50 30 []
Python List Removal: remove(), pop(), clear() and del Statement Quick Reference & Comparison Table
| Removal Syntax | Selection Basis & Return Value | Key Exam Fact |
|---|---|---|
| lst.remove(x) | First matching value; returns None | Raises ValueError if x is not found; O(n) |
| lst.pop(i) | Index i (default -1); returns removed item | Raises IndexError on empty list or bad index |
| lst.clear() | All elements in place; returns None | Equivalent to del lst[:]; keeps original object id |
| del lst[a:b] | Slice or index; statement (no return) | del lst unbinds the variable (NameError next) |
Python List Removal: remove(), pop(), clear() and del Statement Top 20 Bilingual MCQs with Answers
Q1. Which list removal method returns the deleted element?
(कौन-सा लिस्ट रिमूवल मेथड हटाए गए तत्व को वापस लौटाता है?)
(A) pop()
(B) clear()
(C) del
(D) remove()
Correct Answer: (A)
[EN] pop() deletes the element at the specified index (or the last element by default) and returns it, whereas remove() and clear() return None.
[HI] pop() निर्दिष्ट इंडेक्स (या डिफ़ॉल्ट अंतिम) तत्व को हटाकर वापस लौटाता है, जबकि remove() और clear() None लौटाते हैं।
Q2. What exception is raised if x is not found in lst.remove(x)?
(यदि lst.remove(x) में x न मिले तो कौन-सा एक्सेप्शन आता है?)
(A) KeyError
(B) IndexError
(C) ValueError
(D) NameError
Correct Answer: (C)
[EN] remove() searches by value and raises ValueError: list.remove(x): x not in list when absent.
[HI] remove() मान द्वारा खोज करता है और मान न मिलने पर ValueError उत्पन्न करता है।
Q3. What is the output of a = [5, 2, 5, 8]; a.remove(5); print(a)?
(a = [5, 2, 5, 8]; a.remove(5); print(a) का आउटपुट क्या होगा?)
(A) ValueError
(B) [2, 5, 8]
(C) [2, 8]
(D) [5, 2, 8]
Correct Answer: (B)
[EN] remove(5) deletes only the first occurrence of 5 at index 0, leaving the second 5 intact.
[HI] remove(5) केवल पहले मिलने वाले 5 (इंडेक्स 0) को हटाता है, दूसरा 5 लिस्ट में बना रहता है।
Q4. What happens when executing [].pop()?
([].pop() चलाने पर क्या होता है?)
(A) Raises ValueError
(B) Raises IndexError
(C) Returns None
(D) Returns []
Correct Answer: (B)
[EN] Popping from an empty list raises IndexError: pop from empty list.
[HI] खाली लिस्ट पर pop() चलाने से IndexError: pop from empty list आता है।
Q5. What is the time complexity of lst.pop() with no arguments?
(बिना किसी आर्गुमेंट के lst.pop() की समय जटिलता क्या है?)
(A) O(n^2)
(B) O(n)
(C) O(log n)
(D) O(1)
Correct Answer: (D)
[EN] Removing the last item does not require shifting any elements, so pop() runs in O(1) time.
[HI] अंतिम तत्व हटाने पर किसी अन्य तत्व को खिसकाना नहीं पड़ता, अतः यह O(1) में कार्य करता है।
Q6. What is the time complexity of lst.pop(0)?
(lst.pop(0) की समय जटिलता क्या है?)
(A) O(1)
(B) O(log n)
(C) O(1) amortized
(D) O(n)
Correct Answer: (D)
[EN] Removing index 0 shifts all remaining n - 1 elements one slot to the left, requiring O(n) time.
[HI] इंडेक्स 0 से तत्व हटाने के बाद शेष सभी n - 1 तत्वों को बाईं ओर खिसकाना पड़ता है, जिसमें O(n) समय लगता है।
Q7. If a = [1, 2, 3]; b = a; a.clear(), what is the value of b?
(यदि a = [1, 2, 3]; b = a; a.clear() हो, तो b का मान क्या होगा?)
(A) []
(B) [1, 2, 3]
(C) None
(D) NameError
Correct Answer: (A)
[EN] a.clear() empties the shared list object in place, so its alias b also references [].
[HI] a.clear() साझा लिस्ट ऑब्जेक्ट को इन-प्लेस खाली कर देता है, इसलिए b का मान भी [] हो जाता है।
Q8. If a = [1, 2, 3]; b = a; a = [], what is the value of b?
(यदि a = [1, 2, 3]; b = a; a = [] हो, तो b का मान क्या होगा?)
(A) [1, 2, 3]
(B) None
(C) NameError
(D) []
Correct Answer: (A)
[EN] a = [] rebinds variable a to a new empty list without modifying the original object referenced by b.
[HI] a = [] केवल वेरिएबल a को नई खाली लिस्ट से जोड़ता है, जिससे b का मूल मान [1, 2, 3] सुरक्षित रहता है।
Q9. What happens if you execute a = [1, 2]; del a; print(a)?
(a = [1, 2]; del a; print(a) चलाने पर क्या होगा?)
(A) Raises NameError
(B) Prints []
(C) Prints None
(D) Raises IndexError
Correct Answer: (A)
[EN] del a removes the variable binding a from the current scope, so referencing a afterward raises NameError.
[HI] del a वेरिएबल का नाम ही स्कोप से हटा देता है, जिससे आगे print(a) पर NameError आता है।
Q10. Which expression is equivalent to a.clear() in mutating a in place?
(a को इन-प्लेस खाली करने में कौन-सा एक्सप्रेशन a.clear() के समतुल्य है?)
(A) a = []
(B) del a
(C) a.pop()
(D) del a[:]
Correct Answer: (D)
[EN] del a[:] deletes the entire slice of elements inside the existing list object without changing id(a).
[HI] del a[:] मूल लिस्ट ऑब्जेक्ट की id बदले बिना उसके सभी तत्वों का स्लाइस हटा देता है।
Q11. What is the output of nums = [10, 20, 30, 40, 50]; del nums[::2]; print(nums)?
(nums = [10, 20, 30, 40, 50]; del nums[::2]; print(nums) का आउटपुट क्या होगा?)
(A) ValueError
(B) [10, 30, 50]
(C) [20, 40]
(D) [30, 40, 50]
Correct Answer: (C)
[EN] del nums[::2] removes indices 0, 2, and 4 (10, 30, 50), leaving [20, 40].
[HI] del nums[::2] इंडेक्स 0, 2 और 4 (10, 30, 50) को हटा देता है, जिससे [20, 40] बचता है।
Q12. What happens if you try to assign x = (del a[0]) in Python?
(पायथन में x = (del a[0]) लिखने पर क्या होता है?)
(A) x becomes the removed item
(B) SyntaxError
(C) TypeError
(D) x becomes None
Correct Answer: (B)
[EN] del is a statement, not a function or expression, so it cannot be placed on the right-hand side of an assignment.
[HI] del एक स्टेटमेंट है (फंक्शन या एक्सप्रेशन नहीं), इसलिए इसे असाइनमेंट के दाईं ओर नहीं लिखा जा सकता (SyntaxError)।
Q13. What is printed by a = [True, 1, 2]; a.remove(1); print(a)?
(a = [True, 1, 2]; a.remove(1); print(a) से क्या प्रिंट होगा?)
(A) [1, 2]
(B) ValueError
(C) [True, 2]
(D) [True, 1, 2]
Correct Answer: (A)
[EN] In Python, True == 1 evaluates to True, so remove(1) matches and deletes the first element (True).
[HI] पायथन में True == 1 सत्य होता है, इसलिए remove(1) पहले मिलने वाले True को हटा देता है और [1, 2] बचता है।
Q14. Which error occurs on running a = [1, 2]; a.pop(5)?
(a = [1, 2]; a.pop(5) चलाने पर कौन-सी त्रुटि आती है?)
(A) No error, removes last item
(B) ValueError
(C) KeyError
(D) IndexError
Correct Answer: (D)
[EN] Unlike insert() or slicing, pop(5) strictly requires a valid existing index and raises IndexError if out of range.
[HI] insert() या स्लाइसिंग के विपरीत, pop(5) अमान्य इंडेक्स देने पर IndexError उत्पन्न करता है।
Q15. When using a list as a stack (LIFO), which two methods work in O(1) time?
(लिस्ट को स्टैक (LIFO) के रूप में उपयोग करते समय कौन-से दो मेथड O(1) समय में काम करते हैं?)
(A) append() and pop()
(B) append() and pop(0)
(C) insert(0) and pop(0)
(D) insert(0) and pop()
Correct Answer: (A)
[EN] Appending to the tail via append() and popping from the tail via pop() both operate in O(1) time.
[HI] अंत में append() और अंत से pop() दोनों बिना तत्व खिसकाए O(1) समय में कार्य करते हैं।
Q16. Which statement is NOT true about list removal in Python?
(पायथन में लिस्ट से तत्व हटाने के बारे में कौन-सा कथन सत्य नहीं है?)
(A) del lst[1:3] removes multiple elements in one statement
(B) remove() deletes all matching occurrences of a value at once
(C) clear() returns None
(D) pop() without arguments removes the last element
Correct Answer: (B)
[EN] remove(x) only removes the first matching occurrence, not all occurrences of x.
[HI] remove(x) किसी मान की सभी आवृत्तियों को एक साथ नहीं हटाता, बल्कि केवल पहले मिलान को हटाता है।
Q17. Within Python List Removal: remove(), pop(), clear() and del Statement, which removal syntax directly maps to: "First matching value; returns None"?
(Python List Removal: remove(), pop(), clear() and del Statement में कौन सा Removal Syntax सीधे "First matching value; returns None" से संबंधित है?)
(A) lst.pop(i)
(B) del lst[a:b]
(C) lst.clear()
(D) lst.remove(x)
Correct Answer: (D)
[EN] In Python List Removal: remove(), pop(), clear() and del Statement, 'lst.remove(x)' handles 'First matching value; returns None' (Raises ValueError if x is not found; O(n)).
[HI] Python List Removal: remove(), pop(), clear() and del Statement में 'lst.remove(x)' का कार्य/विवरण 'First matching value; returns None' (Raises ValueError if x is not found; O(n)) है।
Q18. Which option accurately describes 'lst.pop(i)' in Python List Removal: remove(), pop(), clear() and del Statement?
(Python List Removal: remove(), pop(), clear() and del Statement के संदर्भ में 'lst.pop(i)' की सही विशेषता कौन सी है?)
(A) Index i (default -1); returns removed item
(B) Slice or index; statement (no return)
(C) All elements in place; returns None
(D) First matching value; returns None
Correct Answer: (A)
[EN] 'lst.pop(i)' provides 'Index i (default -1); returns removed item' — key point: Raises IndexError on empty list or bad index.
[HI] 'lst.pop(i)' का मुख्य विवरण 'Index i (default -1); returns removed item' है (Raises IndexError on empty list or bad index)।
Q19. In Python List Removal: remove(), pop(), clear() and del Statement, identify the correct parameter for the specification: "All elements in place; returns None".
(Python List Removal: remove(), pop(), clear() and del Statement में "All elements in place; returns None" विवरण किस विकल्प के लिए सही है?)
(A) lst.clear()
(B) lst.remove(x)
(C) del lst[a:b]
(D) lst.pop(i)
Correct Answer: (A)
[EN] 'lst.clear()' corresponds to 'All elements in place; returns None' (Equivalent to del lst[:]; keeps original object id).
[HI] 'lst.clear()' का संबंध 'All elements in place; returns None' (Equivalent to del lst[:]; keeps original object id) से है।
Q20. Considering Python List Removal: remove(), pop(), clear() and del Statement, what key behavior defines 'del lst[a:b]'?
(Python List Removal: remove(), pop(), clear() and del Statement में 'del lst[a:b]' की मुख्य भूमिका या मान क्या है?)
(A) All elements in place; returns None
(B) First matching value; returns None
(C) Index i (default -1); returns removed item
(D) Slice or index; statement (no return)
Correct Answer: (D)
[EN] 'del lst[a:b]' represents 'Slice or index; statement (no return)' (del lst unbinds the variable (NameError next)).
[HI] 'del lst[a:b]' का अर्थ/मान 'Slice or index; statement (no return)' है (del lst unbinds the variable (NameError next))।
Python List Removal: remove(), pop(), clear() and del Statement — Exam FAQs
Q1: When should I use del lst[i] instead of lst.pop(i)?
मुझे lst.pop(i) के बजाय del lst[i] का उपयोग कब करना चाहिए?
[EN] Use lst.pop(i) when you need to capture and use the removed value, and use del lst[i] when you simply want to discard the element without storing it (or when deleting an entire slice like del lst[1:4]).
[HI] जब आपको हटाया गया मान किसी वेरिएबल में सहेजना हो तब pop(i) उपयोग करें, और जब मान की आवश्यकता न हो या पूरा स्लाइस हटाना हो तब del lst[i] उपयोग करें।
Q2: How do I remove ALL occurrences of a value x from a list safely?
लिस्ट से किसी मान x की सभी आवृत्तियों को सुरक्षित रूप से कैसे हटाएं?
[EN] Instead of calling remove(x) inside a for loop iterating directly over the same list (which skips elements), use a list comprehension: lst = [item for item in lst if item != x].
[HI] उसी लिस्ट पर लूप चलाते समय तत्व हटाने से इंडेक्स खिसक जाते हैं; सभी आवृत्तियां हटाने के लिए लिस्ट कॉम्प्रिहेंशन [item for item in lst if item != x] सबसे सुरक्षित है।
Q3: Why did a.remove(1) remove False/True or 1.0 in a mixed list?
मिश्रित लिस्ट में a.remove(1) ने True या 1.0 को क्यों हटा दिया?
[EN] remove() checks equality using == rather than type identity (is). Since Python evaluates True == 1 and 1.0 == 1 as True, whichever appears first in the list gets removed.
[HI] remove() टाइप पहचान के बजाय == समानता की जांच करता है। पायथन में True == 1 और 1.0 == 1 सत्य होते हैं, अतः जो पहले मिलता है वही हट जाता है।
Post a Comment
Post a Comment