PGDCA, BCA, B-Tech, MCA, M-Tech, B.Sc computer science राजस्थान सूचना सहायक व कंप्यूटर शिक्षक विशेष नोट्स: इस अध्याय में हम Python List Searching & Sorting: index(), count(), sort(), reverse() (लिस्ट खोज व छंटाई: index(), count(), sort() और reverse()) की संपूर्ण थ्योरी, मुख्य तकनीकी बिंदु, तुलनात्मक तालिका और विगत परीक्षाओं के पैटर्न पर आधारित 20 अति-महत्वपूर्ण बहुविकल्पीय प्रश्न (MCQs) विस्तृत हिंदी व अंग्रेजी व्याख्या सहित पढ़ेंगे।
Overview: Python lists offer index() and count() for O(n) linear searching and sort() and reverse() for in-place ordering. While sort() and reverse() modify the list directly and return None, built-in sorted() and reversed() leave the original list intact.
अध्याय सार: पायथन लिस्ट में O(n) रैखिक खोज के लिए index() और count() तथा इन-प्लेस क्रमबद्धता के लिए sort() और reverse() मेथड्स होते हैं। जहाँ sort() और reverse() मूल लिस्ट को बदलकर None लौटाते हैं, वहीं बिल्ट-इन sorted() और reversed() मूल लिस्ट को सुरक्षित रखते हैं।
Python List Searching & Sorting: index(), count(), sort(), reverse(): Detailed Notes & Architecture (लिस्ट खोज व छंटाई: index(), count(), sort() और reverse())
1. index(x, start, end): Bounded Linear Search (index(x, start, end): सीमित रैखिक खोज)
[EN] The index(x[, start[, end]]) method searches left-to-right from index start (inclusive, default 0) up to end (exclusive, default len(lst)) and returns the zero-based index of the first element satisfying elem == x. It runs in O(n) time. Unlike str.find(), which returns -1 on failure, list.index() raises ValueError if x does not exist within the searched window.
[HI] index(x[, start[, end]]) मेथड start (शामिल) से end (बहिष्कृत) इंडेक्स तक बाएं से दाएं O(n) समय में खोज करता है और elem == x वाले पहले तत्व का इंडेक्स लौटाता है। str.find() के विपरीत (जो -1 लौटाता है), list.index() मान न मिलने पर ValueError उत्पन्न करता है।
2. count(x): Frequency Counting Without Errors (count(x): बिना त्रुटि के आवृत्ति गिनती)
[EN] The count(x) method scans every item in the list in O(n) time and returns an integer indicating how many items satisfy elem == x. If x is not found, count() safely returns 0 and never raises ValueError. Remember Python's numeric/boolean equality rule: [1, True, 1.0, 0, False].count(1) returns 3 because True == 1 and 1.0 == 1.
[HI] count(x) मेथड पूरी लिस्ट को O(n) समय में स्कैन करके यह गिनता है कि कितने तत्व x के बराबर (==) हैं। यदि तत्व मौजूद न हो, तो यह बिना किसी त्रुटि के 0 लौटाता है। चूंकि पायथन में True == 1 और 1.0 == 1 होता है, इसलिए [1, True, 1.0].count(1) का परिणाम 3 आता है।
3. sort(key=None, reverse=False) vs sorted() and Timsort (sort() बनाम sorted() और टिमसॉर्ट एल्गोरिद्म)
[EN] lst.sort() orders elements in place using Timsort (a hybrid of Merge Sort and Insertion Sort) and returns None. Timsort is guaranteed stable (preserving relative order of equal keys), runs in O(n log n) worst-case time, and achieves O(n) on already-sorted data. Its key parameter accepts a single-argument callable (such as key=len or key=str.lower), while reverse=True sorts descending. Built-in sorted(iterable) works on any iterable and returns a new sorted list. Mixed incomparable items like [1, 'a'] raise TypeError.
[HI] lst.sort() टिमसॉर्ट (Merge Sort और Insertion Sort का संकर) द्वारा लिस्ट को इन-प्लेस क्रमबद्ध करता है और None लौटाता है। टिमसॉर्ट एक स्थिर (stable) एल्गोरिद्म है जिसकी वर्स्ट-केस जटिलता O(n log n) और पहले से क्रमबद्ध डेटा पर O(n) होती है। key पैरामीटर कस्टम तुलना फंक्शन (जैसे key=len) और reverse=True अवरोही क्रम के लिए है। बिल्ट-इन sorted() किसी भी इटरेबल से एक नई क्रमबद्ध लिस्ट लौटाता है, जबकि [1, 'a'] जैसे अतुलनीय तत्व TypeError देते हैं।
4. reverse() vs reversed() and Slice Reversal (reverse() बनाम reversed() और स्लाइस रिवर्सल)
[EN] lst.reverse() flips the element sequence in place using two-pointer swaps in O(n) time and O(1) extra space, returning None; note that reverse() merely reverses current order and does NOT sort. By contrast, built-in reversed(seq) does not mutate lst and returns a lazy reverse iterator object (requiring list(reversed(lst)) to materialize), while lst[::-1] allocates a new reversed list copy immediately.
[HI] lst.reverse() टू-पॉइंटर स्वैपिंग से तत्वों का क्रम O(n) समय और O(1) अतिरिक्त मेमोरी में उलट देता है और None लौटाता है; यह लिस्ट को सॉर्ट नहीं करता बल्कि केवल क्रम उलटता है। इसके विपरीत, बिल्ट-इन reversed(seq) मूल लिस्ट को बदले बिना एक लेज़ी रिवर्स इटरेटर लौटाता है, जबकि lst[::-1] तुरंत एक नई उलटी लिस्ट कॉपी बनाता है।
Syntax, Formula & Technical Example (Python List Searching & Sorting: index(), count(), sort(), reverse())
words = ['Banana', 'apple', 'Cherry', 'apple']
print(words.count('apple')) # 2
print(words.index('apple', 2)) # 3 (search starts at index 2)
words.sort(key=str.lower)
print(words) # ['apple', 'apple', 'Banana', 'Cherry']
words.reverse()
print(words) # ['Cherry', 'Banana', 'apple', 'apple']
Python List Searching & Sorting: index(), count(), sort(), reverse() Quick Reference & Comparison Table
| List Method | Return Value & In-Place Effect | Key Exam Fact |
|---|---|---|
| lst.index(x, s, e) | Returns int index of first match | Raises ValueError if x is absent in [s, e); O(n) |
| lst.count(x) | Returns int frequency (0 if absent) | Never raises an error; checks == equality in O(n) |
| lst.sort(key, rev) | Returns None; sorts list in place | Uses stable Timsort: O(n log n) worst, O(n) best |
| lst.reverse() | Returns None; reverses order in place | Does NOT sort; reverses in O(n) time, O(1) space |
Python List Searching & Sorting: index(), count(), sort(), reverse() Top 20 Bilingual MCQs with Answers
Q1. Which sorting algorithm is used internally by Python's list.sort() and sorted()?
(पायथन के list.sort() और sorted() आंतरिक रूप से किस सॉर्टिंग एल्गोरिद्म का उपयोग करते हैं?)
(A) Timsort
(B) Heapsort
(C) Bubble Sort
(D) Quicksort
Correct Answer: (A)
[EN] Python uses Timsort, a stable hybrid sorting algorithm derived from Merge Sort and Insertion Sort.
[HI] पायथन टिमसॉर्ट का उपयोग करता है, जो मर्ज सॉर्ट और इंसर्शन सॉर्ट का एक स्थिर (stable) संकर एल्गोरिद्म है।
Q2. What does a = [3, 1, 2]; x = a.sort(); print(x) output?
(a = [3, 1, 2]; x = a.sort(); print(x) का आउटपुट क्या होगा?)
(A) None
(B) True
(C) [3, 2, 1]
(D) [1, 2, 3]
Correct Answer: (A)
[EN] list.sort() sorts the list in place and returns None; use sorted(a) if you need a new sorted list returned.
[HI] list.sort() लिस्ट को इन-प्लेस सॉर्ट करता है और None लौटाता है; नई लिस्ट पाने के लिए sorted(a) उपयोग करें।
Q3. What happens when you execute [10, 20, 30].index(99)?
([10, 20, 30].index(99) चलाने पर क्या होता है?)
(A) Returns -1
(B) Raises IndexError
(C) Raises ValueError
(D) Returns None
Correct Answer: (C)
[EN] Unlike string.find() which returns -1, list.index() raises ValueError when the element is not present.
[HI] string.find() के विपरीत (जो -1 देता है), list.index() तत्व न मिलने पर ValueError देता है।
Q4. What is the output of a = [10, 20, 10, 30, 10]; print(a.index(10, 1, 4))?
(a = [10, 20, 10, 30, 10]; print(a.index(10, 1, 4)) का आउटपुट क्या होगा?)
(A) 4
(B) 0
(C) ValueError
(D) 2
Correct Answer: (D)
[EN] Searching starts at index 1 (inclusive) up to index 4 (exclusive); the first 10 in that range is at index 2.
[HI] खोज इंडेक्स 1 (शामिल) से 4 (बहिष्कृत) के बीच होती है, जहाँ पहला 10 इंडेक्स 2 पर मिलता है।
Q5. What is printed by print([0, False, 1, True].count(0))?
(print([0, False, 1, True].count(0)) से क्या प्रिंट होगा?)
(A) 2
(B) 0
(C) 4
(D) 1
Correct Answer: (A)
[EN] Since False == 0 evaluates to True in Python, count(0) counts both 0 and False, returning 2.
[HI] पायथन में False == 0 सत्य होता है, इसलिए count(0) दोनों (0 और False) को गिनकर 2 लौटाता है।
Q6. What happens when running [1, '2', 3].sort() in Python 3?
(पायथन 3 में [1, '2', 3].sort() चलाने पर क्या होता है?)
(A) Raises TypeError
(B) Converts '2' to 2 automatically
(C) Raises ValueError
(D) Sorts as [1, 3, '2']
Correct Answer: (A)
[EN] Python 3 forbids ordering comparisons (<) between int and str, raising TypeError: '<' not supported between instances of 'str' and 'int'.
[HI] पायथन 3 में int और str के बीच तुलना (<) अनुमत नहीं है, जिससे TypeError उत्पन्न होता है।
Q7. What is the output of a = [3, 1, 4, 2]; a.reverse(); print(a)?
(a = [3, 1, 4, 2]; a.reverse(); print(a) का आउटपुट क्या होगा?)
(A) [1, 2, 3, 4]
(B) [2, 4, 1, 3]
(C) None
(D) [4, 3, 2, 1]
Correct Answer: (B)
[EN] reverse() strictly reverses the current physical order of elements in place without sorting them.
[HI] reverse() तत्वों को सॉर्ट नहीं करता, बल्कि केवल उनके वर्तमान क्रम को इन-प्लेस उलट देता है।
Q8. What does built-in reversed([1, 2, 3]) return directly?
(बिल्ट-इन reversed([1, 2, 3]) सीधे क्या लौटाता है?)
(A) A reverse iterator object
(B) (3, 2, 1)
(C) None
(D) [3, 2, 1]
Correct Answer: (A)
[EN] reversed() returns a lazy list_reverseiterator; pass it to list() or iterate over it to consume values.
[HI] reversed() एक लेज़ी रिवर्स इटरेटर ऑब्जेक्ट लौटाता है; लिस्ट पाने के लिए list(reversed(...)) लिखना होता है।
Q9. How do you sort ['a', 'bbb', 'cc'] from longest string to shortest?
(['a', 'bbb', 'cc'] को सबसे लंबी स्ट्रिंग से सबसे छोटी के क्रम में कैसे सॉर्ट करेंगे?)
(A) lst.sort(key=len, reverse=True)
(B) lst.sort(key=len)
(C) lst.reverse(key=len)
(D) lst.sort(reverse=len)
Correct Answer: (A)
[EN] key=len compares strings by length, and reverse=True orders them descending from longest to shortest.
[HI] key=len लंबाई के आधार पर तुलना करता है और reverse=True उसे अवरोही (बड़े से छोटे) क्रम में सजाता है।
Q10. What is the worst-case time complexity of Timsort (list.sort())?
(टिमसॉर्ट (list.sort()) की वर्स्ट-केस समय जटिलता क्या है?)
(A) O(n log n)
(B) O(n^2)
(C) O(log n)
(D) O(n)
Correct Answer: (A)
[EN] Timsort guarantees O(n log n) worst-case time complexity and O(n) best-case time on nearly sorted lists.
[HI] टिमसॉर्ट वर्स्ट-केस में O(n log n) और लगभग क्रमबद्ध डेटा पर O(n) समय जटिलता की गारंटी देता है।
Q11. What does ['x', 'y'].count('z') return?
(['x', 'y'].count('z') क्या लौटाता है?)
(A) ValueError
(B) None
(C) 0
(D) -1
Correct Answer: (C)
[EN] count() returns 0 whenever the queried element does not appear in the list.
[HI] जब खोजा गया तत्व लिस्ट में नहीं होता, तब count() बिना किसी त्रुटि के 0 लौटाता है।
Q12. Which difference between sort() and sorted() is accurate?
(sort() और sorted() के बीच कौन-सा अंतर सही है?)
(A) sorted() mutates the original list in place
(B) sorted() accepts any iterable and returns a new list; sort() only works on lists in place
(C) sort() is O(n^2) while sorted() is O(n log n)
(D) sort() accepts tuples as well as lists
Correct Answer: (B)
[EN] sort() is a list method returning None; sorted() is a built-in function working on tuples, strings, sets, or dicts.
[HI] sort() केवल लिस्ट का मेथड है जो None लौटाता है, जबकि sorted() किसी भी इटरेबल पर काम करके नई लिस्ट लौटाता है।
Q13. What does ['b', 'A', 'c'].sort() produce under default ASCII/Unicode comparison?
(डिफ़ॉल्ट ASCII/यूनिकोड तुलना में ['b', 'A', 'c'].sort() से क्या बनेगा?)
(A) ['A', 'c', 'b']
(B) ['c', 'b', 'A']
(C) ['b', 'c', 'A']
(D) ['A', 'b', 'c']
Correct Answer: (D)
[EN] Uppercase ASCII letters (A=65) have smaller code points than lowercase letters (b=98), so 'A' comes first.
[HI] बड़े अक्षरों का ASCII मान (A=65) छोटे अक्षरों (b=98) से कम होता है, इसलिए 'A' सबसे पहले आता है।
Q14. In Python, what does 'a sorting algorithm is stable' mean for lst.sort(key=len)?
(lst.sort(key=len) के संदर्भ में सॉर्टिंग का 'स्थिर' (stable) होने का क्या अर्थ है?)
(A) Elements with equal length keep their original relative order
(B) Sorting requires O(1) memory
(C) Duplicate items are automatically deleted
(D) The list cannot be modified after sorting
Correct Answer: (A)
[EN] Timsort stability guarantees that ties under the key function preserve their initial left-to-right ordering.
[HI] स्थिरता (stability) का अर्थ है कि समान key मान वाले तत्वों का मूल सापेक्ष क्रम सॉर्ट के बाद भी बना रहता है।
Q15. What is the auxiliary space complexity of lst.reverse()?
(lst.reverse() की अतिरिक्त मेमोरी (auxiliary space) जटिलता क्या है?)
(A) O(log n)
(B) O(n^2)
(C) O(n)
(D) O(1)
Correct Answer: (D)
[EN] reverse() swaps symmetric pointer pairs in place using only constant extra memory.
[HI] reverse() केवल दो पॉइंटर्स की मदद से तत्वों को इन-प्लेस स्वैप करता है, जिससे O(1) अतिरिक्त मेमोरी लगती है।
Q16. Which statement is NOT true about list searching and sorting methods?
(लिस्ट खोज और छंटाई मेथड्स के बारे में कौन-सा कथन सत्य नहीं है?)
(A) sorted() does not mutate the original list
(B) list.index() returns -1 if the item is not found
(C) list.count() returns 0 if the item is not found
(D) list.sort() returns None
Correct Answer: (B)
[EN] list.index() raises ValueError on failure; returning -1 is the behavior of str.find(), not list.index().
[HI] तत्व न मिलने पर list.index() -1 नहीं लौटाता बल्कि ValueError देता है; -1 लौटाना str.find() का गुण है।
Q17. Within Python List Searching & Sorting: index(), count(), sort(), reverse(), which list method directly maps to: "Returns int index of first match"?
(Python List Searching & Sorting: index(), count(), sort(), reverse() में कौन सा List Method सीधे "Returns int index of first match" से संबंधित है?)
(A) lst.reverse()
(B) lst.index(x, s, e)
(C) lst.count(x)
(D) lst.sort(key, rev)
Correct Answer: (B)
[EN] In Python List Searching & Sorting: index(), count(), sort(), reverse(), 'lst.index(x, s, e)' handles 'Returns int index of first match' (Raises ValueError if x is absent in [s, e); O(n)).
[HI] Python List Searching & Sorting: index(), count(), sort(), reverse() में 'lst.index(x, s, e)' का कार्य/विवरण 'Returns int index of first match' (Raises ValueError if x is absent in [s, e); O(n)) है।
Q18. Which option accurately describes 'lst.count(x)' in Python List Searching & Sorting: index(), count(), sort(), reverse()?
(Python List Searching & Sorting: index(), count(), sort(), reverse() के संदर्भ में 'lst.count(x)' की सही विशेषता कौन सी है?)
(A) Returns int frequency (0 if absent)
(B) Returns int index of first match
(C) Returns None; reverses order in place
(D) Returns None; sorts list in place
Correct Answer: (A)
[EN] 'lst.count(x)' provides 'Returns int frequency (0 if absent)' — key point: Never raises an error; checks == equality in O(n).
[HI] 'lst.count(x)' का मुख्य विवरण 'Returns int frequency (0 if absent)' है (Never raises an error; checks == equality in O(n))।
Q19. In Python List Searching & Sorting: index(), count(), sort(), reverse(), identify the correct parameter for the specification: "Returns None; sorts list in place".
(Python List Searching & Sorting: index(), count(), sort(), reverse() में "Returns None; sorts list in place" विवरण किस विकल्प के लिए सही है?)
(A) lst.count(x)
(B) lst.sort(key, rev)
(C) lst.reverse()
(D) lst.index(x, s, e)
Correct Answer: (B)
[EN] 'lst.sort(key, rev)' corresponds to 'Returns None; sorts list in place' (Uses stable Timsort: O(n log n) worst, O(n) best).
[HI] 'lst.sort(key, rev)' का संबंध 'Returns None; sorts list in place' (Uses stable Timsort: O(n log n) worst, O(n) best) से है।
Q20. Considering Python List Searching & Sorting: index(), count(), sort(), reverse(), what key behavior defines 'lst.reverse()'?
(Python List Searching & Sorting: index(), count(), sort(), reverse() में 'lst.reverse()' की मुख्य भूमिका या मान क्या है?)
(A) Returns None; sorts list in place
(B) Returns int index of first match
(C) Returns int frequency (0 if absent)
(D) Returns None; reverses order in place
Correct Answer: (D)
[EN] 'lst.reverse()' represents 'Returns None; reverses order in place' (Does NOT sort; reverses in O(n) time, O(1) space).
[HI] 'lst.reverse()' का अर्थ/मान 'Returns None; reverses order in place' है (Does NOT sort; reverses in O(n) time, O(1) space)।
Python List Searching & Sorting: index(), count(), sort(), reverse() — Exam FAQs
Q1: Why does Python NOT provide a list.find() method like str.find()?
पायथन str.find() की तरह list.find() मेथड क्यों नहीं देता?
[EN] If list.find() returned -1 on failure, callers might accidentally pass -1 as a negative list index (selecting the last item!) instead of catching a missing element. Raising ValueError in index() prevents this bug.
[HI] यदि list.find() -1 लौटाता, तो प्रोग्रामर गलती से -1 को अंतिम तत्व का ऋणात्मक इंडेक्स समझकर उपयोग कर सकते थे। index() में ValueError आने से यह बग रुक जाता है।
Q2: How do I sort a list of strings case-insensitively?
स्ट्रिंग्स की लिस्ट को केस-इनसेंसिटिव (बड़े-छोटे अक्षर भेद बिना) कैसे सॉर्ट करें?
[EN] Pass key=str.lower (or key=str.casefold for Unicode) to sort(): words.sort(key=str.lower). Do not write str.lower() with parentheses, because sort() needs the function object itself.
[HI] sort() में key=str.lower पास करें, जैसे words.sort(key=str.lower)। ध्यान रहे कि str.lower के बाद कोष्ठक () न लगाएं।
Q3: Does sorted(lst) modify lst?
क्या sorted(lst) मूल लिस्ट lst में बदलाव करता है?
[EN] No. sorted(lst) builds and returns a brand-new sorted list while leaving lst completely untouched. Use lst.sort() only when in-place mutation is desired.
[HI] नहीं। sorted(lst) मूल लिस्ट को छुए बिना एक नई क्रमबद्ध लिस्ट बनाता और लौटाता है। मूल लिस्ट को बदलने के लिए lst.sort() उपयोग करें।
Post a Comment
Post a Comment