Python List Indexing, Negative Indexing and Slicing Notes in Hindi & English : लिस्ट इंडेक्सिंग, सब-लिस्ट स्लाइसिंग और तत्वों में बदलाव और 20 महत्वपूर्ण MCQ प्रश्न (Rajasthan IT Exam)

Post a Comment

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

Overview: Python lists support zero-based positive indexing, negative indexing from the tail, and slicing via lst[start:stop:step] to extract sub-lists in O(k) time. Because lists are mutable, individual indices and slice ranges can also be reassigned in place to update, insert, or delete elements.

अध्याय सार: पायथन लिस्ट शून्य-आधारित धनात्मक इंडेक्सिंग, अंत से ऋणात्मक इंडेक्सिंग और lst[start:stop:step] सिंटैक्स द्वारा सब-लिस्ट स्लाइसिंग का समर्थन करती है। चूंकि लिस्ट म्यूटेबल (परिवर्तनशील) होती है, इसलिए इंडेक्स और स्लाइस असाइनमेंट द्वारा तत्वों को बदला, जोड़ा या हटाया जा सकता है।

Python List Indexing, Negative Indexing and Slicing: Detailed Notes & Architecture (लिस्ट इंडेक्सिंग, सब-लिस्ट स्लाइसिंग और तत्वों में बदलाव)

1. Positive and Negative Indexing Internals (धनात्मक और ऋणात्मक इंडेक्सिंग की कार्यप्रणाली)

[EN] In CPython, a list (PyListObject) stores a contiguous C array of PyObject pointers, allowing O(1) constant-time random access by index. Positive indices start at 0 for the first item up to len(lst) - 1, while negative indices count backward starting at -1 for the last item down to -len(lst). Internally, a negative index i is translated to len(lst) + i before lookup. Accessing an index outside [-len(lst), len(lst) - 1] immediately raises IndexError: list index out of range.

[HI] CPython में लिस्ट (PyListObject) ऑब्जेक्ट पॉइंटर्स का एक सतत ऐरे स्टोर करती है, जिससे किसी भी इंडेक्स तक O(1) समय में सीधी पहुंच मिलती है। धनात्मक इंडेक्स पहले तत्व के लिए 0 से शुरू होकर len(lst) - 1 तक जाता है, जबकि ऋणात्मक इंडेक्स अंतिम तत्व -1 से शुरू होकर -len(lst) तक गिनता है। आंतरिक रूप से ऋणात्मक इंडेक्स i को len(lst) + i में बदला जाता है। मान्य सीमा से बाहर का इंडेक्स एक्सेस करने पर IndexError: list index out of range त्रुटि आती है।

2. Slicing Syntax and Boundary Clamping (स्लाइसिंग सिंटैक्स और सीमा क्लैम्पिंग)

[EN] The slice expression lst[start:stop:step] extracts elements beginning at start (inclusive) up to stop (exclusive) with stride step. When omitted, defaults are start=0, stop=len(lst), and step=1 for positive strides, whereas a negative step reverses traversal with defaults start=-1 and stop before index 0. Unlike single indexing, out-of-range slice bounds never raise IndexError; Python silently clamps them to valid boundaries and returns an empty list [] if the range is empty. Setting step=0 raises ValueError: slice step cannot be zero.

[HI] स्लाइस एक्सप्रेशन lst[start:stop:step] start (शामिल) से शुरू होकर stop (बहिष्कृत) से ठीक पहले तक step अंतराल पर तत्व निकालता है। मान न देने पर धनात्मक step में डिफ़ॉल्ट start=0, stop=len(lst) और step=1 होता है, जबकि ऋणात्मक step होने पर लिस्ट उलटी दिशा में पढ़ी जाती है। सिंगल इंडेक्सिंग के विपरीत, सीमा से बाहर के स्लाइस मान IndexError नहीं देते बल्कि स्वचालित रूप से मान्य सीमा में क्लैम्प हो जाते हैं और खाली होने पर [] लौटाते हैं। यदि step=0 दिया जाए तो ValueError उत्पन्न होता है।

3. Shallow Copy Behavior of Slices (स्लाइसिंग का शैलो कॉपी (Shallow Copy) व्यवहार)

[EN] Evaluating lst[:] or any slice allocates a brand-new outer list object of length k in O(k) time and space, meaning id(lst[:]) differs from id(lst). Reassigning elements in the sliced copy does not alter top-level references in the original list. However, slicing performs a shallow copy rather than a deep copy: if the original list contains mutable nested objects such as inner lists or dictionaries, both lists still share pointers to the exact same inner objects. Modifying a nested list via the slice therefore reflects in the original list unless copy.deepcopy() is used.

[HI] lst[:] या कोई भी स्लाइस चलाने पर O(k) समय और मेमोरी में एक नया बाहरी लिस्ट ऑब्जेक्ट बनता है, जिससे id(lst[:]) मूल लिस्ट से अलग होता है। स्लाइस की गई कॉपी में शीर्ष-स्तरीय तत्व बदलने से मूल लिस्ट पर असर नहीं पड़ता। हालांकि, स्लाइसिंग केवल शैलो कॉपी बनाती है, डीप कॉपी नहीं: यदि मूल लिस्ट के भीतर कोई म्यूटेबल नेस्टेड लिस्ट है, तो दोनों लिस्ट उसी एक आंतरिक ऑब्जेक्ट को साझा करती हैं। पूर्ण स्वतंत्रता के लिए copy.deepcopy() का उपयोग करना पड़ता है।

4. Element Mutation and Slice Assignment (तत्वों में बदलाव और स्लाइस असाइनमेंट)

[EN] Because Python lists are mutable, lst[i] = val overwrites the reference at index i in O(1) time. Contiguous slice assignment such as lst[1:3] = [9, 8, 7] replaces the selected slice with any iterable, automatically growing or shrinking the list as needed—even assigning [] deletes the slice. When using an extended slice with step != 1 (for example lst[::2] = [10, 20]), the assigned iterable must contain the exact same number of elements as the slice, otherwise Python raises ValueError.

[HI] पायथन लिस्ट म्यूटेबल होती हैं, इसलिए lst[i] = val द्वारा किसी इंडेक्स का मान O(1) समय में सीधे बदला जा सकता है। सतत स्लाइस असाइनमेंट जैसे lst[1:3] = [9, 8, 7] चुने गए हिस्से को किसी भी लंबाई के इटरेबल से बदल देता है, जिससे लिस्ट बड़ी या छोटी हो सकती है (और [] देने पर वह हिस्सा हट जाता है)। यदि step != 1 वाला एक्सटेंडेड स्लाइस (जैसे lst[::2]) बदला जा रहा हो, तो नए मानों की संख्या स्लाइस के बराबर होना अनिवार्य है, अन्यथा ValueError आता है।

Syntax, Formula & Technical Example (Python List Indexing, Negative Indexing and Slicing)

nums = [10, 20, 30, 40, 50]
print(nums[-1], nums[-4])       # 50 20
print(nums[1:4], nums[10:20])   # [20, 30, 40] []
print(nums[::-1])               # [50, 40, 30, 20, 10]
nums[1:3] = [25, 35, 38]
print(nums)                     # [10, 25, 35, 38, 40, 50]
nums[::2] = [1, 2, 3]
print(nums)                     # [1, 25, 2, 38, 3, 50]

Python List Indexing, Negative Indexing and Slicing Quick Reference & Comparison Table

Slice / Index ExpressionOutput on a = [10, 20, 30, 40, 50]Key Exam Fact
a[-2]40Negative index counts from end (-1 is last element)
a[1:4][20, 30, 40]Start index 1 is inclusive; stop index 4 is exclusive
a[::-1][50, 40, 30, 20, 10]Step -1 returns a reversed shallow copy of the list
a[10:20][] (empty list)Out-of-bounds slices clamp silently without IndexError

Python List Indexing, Negative Indexing and Slicing Top 20 Bilingual MCQs with Answers

Q1. What does negative index -1 always refer to in a non-empty Python list?

(एक गैर-खाली पायथन लिस्ट में ऋणात्मक इंडेक्स -1 हमेशा किसे संदर्भित करता है?)

(A) The last element of the list

(B) It raises IndexError

(C) The second-to-last element

(D) The first element of the list

Correct Answer: (A)

[EN] Negative indexing starts at -1 for the final element, equivalent to lst[len(lst) - 1].

[HI] ऋणात्मक इंडेक्सिंग -1 से अंतिम तत्व के लिए शुरू होती है, जो lst[len(lst) - 1] के समतुल्य है।

Q2. What is the time complexity of accessing lst[i] by index in CPython?

(CPython में इंडेक्स द्वारा lst[i] को एक्सेस करने की समय जटिलता क्या है?)

(A) O(1)

(B) O(log n)

(C) O(n)

(D) O(n log n)

Correct Answer: (A)

[EN] CPython lists store pointers in a contiguous array, allowing direct O(1) offset calculation.

[HI] CPython लिस्ट कंटिग्युअस ऐरे में पॉइंटर्स रखती है, जिससे O(1) समय में सीधा एक्सेस मिलता है।

Q3. What is the output of a = [10, 20, 30, 40]; print(a[10:15])?

(a = [10, 20, 30, 40]; print(a[10:15]) का आउटपुट क्या होगा?)

(A) []

(B) IndexError

(C) [40]

(D) None

Correct Answer: (A)

[EN] Slicing clamps out-of-range start and stop values safely and returns an empty list instead of raising IndexError.

[HI] स्लाइसिंग सीमा से बाहर के इंडेक्स को सुरक्षित रूप से क्लैम्प कर देती है और IndexError के बजाय [] लौटाती है।

Q4. Which exception is raised when executing lst[::0]?

(lst[::0] चलाने पर कौन-सा एक्सेप्शन उत्पन्न होता है?)

(A) IndexError

(B) ValueError

(C) ZeroDivisionError

(D) TypeError

Correct Answer: (B)

[EN] Python prohibits a slice step of zero because traversal cannot progress, raising ValueError: slice step cannot be zero.

[HI] स्लाइस स्टेप 0 होने पर आगे बढ़ना असंभव है, इसलिए पायथन ValueError उत्पन्न करता है।

Q5. What will x = [1, 2, 3, 4, 5]; print(x[-4:-1]) output?

(x = [1, 2, 3, 4, 5]; print(x[-4:-1]) का आउटपुट क्या होगा?)

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

(B) [2, 3, 4]

(C) [4, 3, 2]

(D) [1, 2, 3]

Correct Answer: (B)

[EN] Index -4 is 2 (inclusive) and index -1 is 5 (exclusive) with default step +1, producing [2, 3, 4].

[HI] इंडेक्स -4 पर 2 (शामिल) और -1 पर 5 (बहिष्कृत) है, अतः डिफ़ॉल्ट +1 स्टेप से [2, 3, 4] मिलता है।

Q6. What is the result of a = [1, 2, 3]; a[1:2] = [8, 9]; print(a)?

(a = [1, 2, 3]; a[1:2] = [8, 9]; print(a) का परिणाम क्या होगा?)

(A) ValueError

(B) [1, 8, 3]

(C) [1, [8, 9], 3]

(D) [1, 8, 9, 3]

Correct Answer: (D)

[EN] Contiguous slice assignment replaces slice [2] with items 8 and 9, dynamically resizing the list to [1, 8, 9, 3].

[HI] सतत स्लाइस असाइनमेंट स्लाइस [2] को 8 और 9 से बदलकर लिस्ट का आकार [1, 8, 9, 3] कर देता है।

Q7. Why does a = [1, 2, 3, 4]; a[::2] = [9] raise a ValueError?

(a = [1, 2, 3, 4]; a[::2] = [9] चलाने पर ValueError क्यों आता है?)

(A) Step value 2 is illegal in assignment

(B) Lists are immutable during slicing

(C) Slices can only accept tuples

(D) Extended slice assignment requires the new iterable to have the exact same length as the slice

Correct Answer: (D)

[EN] Extended slices (step != 1) cannot resize the list, so replacing 2 elements (a[0], a[2]) with 1 element fails with ValueError.

[HI] step != 1 वाले एक्सटेंडेड स्लाइस लिस्ट का आकार नहीं बदल सकते, इसलिए 2 तत्वों के स्थान पर 1 तत्व देने से ValueError आता है।

Q8. What does b = a[:] create when a = [[1, 2], 3]?

(जब a = [[1, 2], 3] हो, तो b = a[:] क्या बनाता है?)

(A) An alias referencing the exact same outer list

(B) A deep copy of a

(C) A shallow copy of a

(D) A tuple copy of a

Correct Answer: (C)

[EN] a[:] allocates a new outer list (a is not b) while keeping shared references to nested mutable objects like [1, 2].

[HI] a[:] एक नई बाहरी लिस्ट (शैलो कॉपी) बनाता है, लेकिन नेस्टेड [1, 2] लिस्ट का रेफरेंस साझा रहता है।

Q9. What is printed by a = [10, 20, 30, 40]; print(a[3:1])?

(a = [10, 20, 30, 40]; print(a[3:1]) से क्या प्रिंट होगा?)

(A) IndexError

(B) [40, 30]

(C) []

(D) [30, 20]

Correct Answer: (C)

[EN] With default positive step +1, start=3 is already past stop=1, so the slice produces an empty list [].

[HI] धनात्मक स्टेप +1 के साथ start=3 पहले ही stop=1 से आगे है, इसलिए आउटपुट [] होता है।

Q10. What is printed by a = [10, 20, 30, 40]; print(a[3:1:-1])?

(a = [10, 20, 30, 40]; print(a[3:1:-1]) से क्या प्रिंट होगा?)

(A) [30, 40]

(B) [40, 30, 20]

(C) [40, 30]

(D) []

Correct Answer: (C)

[EN] Starting at index 3 (40) and stepping -1 until before index 1 (20) yields [40, 30].

[HI] इंडेक्स 3 (40) से शुरू होकर -1 कदम चलते हुए इंडेक्स 1 (20) से पहले रुकने पर [40, 30] मिलता है।

Q11. If a = [1, 2, 3, 4], what happens after executing a[1:3] = []?

(यदि a = [1, 2, 3, 4] है, तो a[1:3] = [] चलाने के बाद क्या होगा?)

(A) IndexError is raised

(B) a becomes [1, 4]

(C) a becomes [1, [], 4]

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

Correct Answer: (B)

[EN] Assigning an empty iterable [] to a contiguous slice removes those elements from the list in place.

[HI] किसी सतत स्लाइस को खाली इटरेबल [] असाइन करने से वे तत्व लिस्ट से हट जाते हैं।

Q12. Which expression accesses the character 'y' in data = ['hi', ['py', 99]]?

(data = ['hi', ['py', 99]] में 'y' अक्षर प्राप्त करने का सही एक्सप्रेशन कौन-सा है?)

(A) data[2][1][2]

(B) data[1][1][0]

(C) data[1][0][1]

(D) data[-1][-1][1]

Correct Answer: (C)

[EN] data[1] is ['py', 99], data[1][0] is 'py', and index 1 of 'py' is 'y'.

[HI] data[1] से ['py', 99], data[1][0] से 'py', और इंडेक्स 1 से 'y' प्राप्त होता है।

Q13. For a list of length 5, which index expression raises IndexError?

(5 लंबाई वाली लिस्ट के लिए कौन-सा इंडेक्स एक्सप्रेशन IndexError देगा?)

(A) lst[-5]

(B) lst[5:10]

(C) lst[4]

(D) lst[-6]

Correct Answer: (D)

[EN] Valid single indices for length 5 are 0..4 and -5..-1; -6 is out of bounds, while lst[5:10] is a slice that returns [].

[HI] लंबाई 5 के लिए मान्य इंडेक्स 0..4 और -5..-1 हैं; -6 सीमा से बाहर है, जबकि lst[5:10] स्लाइस होने से [] लौटाता है।

Q14. What does slice(1, 5, 2) represent when passed to lst[slice(1, 5, 2)]?

(lst[slice(1, 5, 2)] में slice(1, 5, 2) किसे दर्शाता है?)

(A) lst[0:5:2]

(B) lst[1:4:2]

(C) lst[1:2:5]

(D) lst[1:5:2]

Correct Answer: (D)

[EN] Python internally converts colon slice notation lst[start:stop:step] into a built-in slice(start, stop, step) object.

[HI] पायथन आंतरिक रूप से lst[start:stop:step] को बिल्ट-इन slice(start, stop, step) ऑब्जेक्ट में बदलता है।

Q15. What is the boolean result of (a == a[:]) and (a is a[:]) for a = [1, 2]?

(a = [1, 2] के लिए (a == a[:]) and (a is a[:]) का परिणाम क्या होगा?)

(A) False

(B) True

(C) None

(D) TypeError

Correct Answer: (A)

[EN] a == a[:] is True (equal contents), whereas a is a[:] is False because slicing creates a distinct list object.

[HI] a == a[:] सत्य है क्योंकि मान समान हैं, परंतु a is a[:] असत्य है क्योंकि स्लाइस नया ऑब्जेक्ट बनाता है।

Q16. Which statement is NOT true about Python list indexing and slicing?

(पायथन लिस्ट इंडेक्सिंग और स्लाइसिंग के बारे में कौन-सा कथन सत्य नहीं है?)

(A) Single index access runs in O(1) time

(B) Out-of-range slice stop values always raise IndexError

(C) Negative step traverses elements from right to left

(D) Slicing creates a shallow copy of the selected items

Correct Answer: (B)

[EN] Out-of-range slice bounds never raise IndexError; they automatically clamp to the valid list boundaries.

[HI] सीमा से बाहर के स्लाइस मान IndexError नहीं देते; वे स्वचालित रूप से मान्य सीमा तक क्लैम्प हो जाते हैं।

Q17. Within Python List Indexing, Negative Indexing and Slicing, which slice / index expression directly maps to: "40"?

(Python List Indexing, Negative Indexing and Slicing में कौन सा Slice / Index Expression सीधे "40" से संबंधित है?)

(A) a[10:20]

(B) a[-2]

(C) a[::-1]

(D) a[1:4]

Correct Answer: (B)

[EN] In Python List Indexing, Negative Indexing and Slicing, 'a[-2]' handles '40' (Negative index counts from end (-1 is last element)).

[HI] Python List Indexing, Negative Indexing and Slicing में 'a[-2]' का कार्य/विवरण '40' (Negative index counts from end (-1 is last element)) है।

Q18. Which option accurately describes 'a[1:4]' in Python List Indexing, Negative Indexing and Slicing?

(Python List Indexing, Negative Indexing and Slicing के संदर्भ में 'a[1:4]' की सही विशेषता कौन सी है?)

(A) 40

(B) [20, 30, 40]

(C) [50, 40, 30, 20, 10]

(D) [] (empty list)

Correct Answer: (B)

[EN] 'a[1:4]' provides '[20, 30, 40]' — key point: Start index 1 is inclusive; stop index 4 is exclusive.

[HI] 'a[1:4]' का मुख्य विवरण '[20, 30, 40]' है (Start index 1 is inclusive; stop index 4 is exclusive)।

Q19. In Python List Indexing, Negative Indexing and Slicing, identify the correct parameter for the specification: "[50, 40, 30, 20, 10]".

(Python List Indexing, Negative Indexing and Slicing में "[50, 40, 30, 20, 10]" विवरण किस विकल्प के लिए सही है?)

(A) a[-2]

(B) a[1:4]

(C) a[10:20]

(D) a[::-1]

Correct Answer: (D)

[EN] 'a[::-1]' corresponds to '[50, 40, 30, 20, 10]' (Step -1 returns a reversed shallow copy of the list).

[HI] 'a[::-1]' का संबंध '[50, 40, 30, 20, 10]' (Step -1 returns a reversed shallow copy of the list) से है।

Q20. Considering Python List Indexing, Negative Indexing and Slicing, what key behavior defines 'a[10:20]'?

(Python List Indexing, Negative Indexing and Slicing में 'a[10:20]' की मुख्य भूमिका या मान क्या है?)

(A) [50, 40, 30, 20, 10]

(B) 40

(C) [] (empty list)

(D) [20, 30, 40]

Correct Answer: (C)

[EN] 'a[10:20]' represents '[] (empty list)' (Out-of-bounds slices clamp silently without IndexError).

[HI] 'a[10:20]' का अर्थ/मान '[] (empty list)' है (Out-of-bounds slices clamp silently without IndexError)।

Python List Indexing, Negative Indexing and Slicing — Exam FAQs

Q1: Why does lst[100] raise IndexError while lst[100:200] returns []?

lst[100] से IndexError क्यों आता है जबकि lst[100:200] खाली लिस्ट [] क्यों लौटाता है?

[EN] Single indexing expects one concrete element at that exact offset, so missing offsets fail with IndexError. Slicing computes a range intersection with [0, len(lst)], safely clamping invalid bounds to produce an empty subsequence.

[HI] सिंगल इंडेक्सिंग उस सटीक स्थान पर एक तत्व मांगती है, इसलिए सीमा से बाहर होने पर IndexError आता है। स्लाइसिंग [0, len(lst)] सीमा के साथ क्लैम्पिंग करके सुरक्षित रूप से खाली लिस्ट लौटाती है।

Q2: Does b = a[:] protect nested lists inside a from changes made via b?

क्या b = a[:] करने से a के भीतर मौजूद नेस्टेड लिस्ट b के बदलावों से सुरक्षित रहती हैं?

[EN] No. Slicing creates a shallow copy where only the outer list container is duplicated. Inner mutable objects such as nested lists still share the same memory references; use copy.deepcopy(a) for full isolation.

[HI] नहीं। स्लाइसिंग केवल बाहरी लिस्ट की शैलो कॉपी बनाती है। अंदर की म्यूटेबल लिस्ट अभी भी समान रेफरेंस साझा करती हैं; पूर्ण सुरक्षा के लिए copy.deepcopy(a) उपयोग करें।

Q3: Why does a[::2] = [1] fail with ValueError even though a[1:3] = [1] works?

a[1:3] = [1] काम करता है, लेकिन a[::2] = [1] ValueError के साथ विफल क्यों होता है?

[EN] Contiguous slices (step=1) represent a continuous memory block that Python can resize arbitrarily. Extended slices (step != 1) target scattered slots, so Python strictly enforces a 1-to-1 length match.

[HI] step=1 वाले सतत स्लाइस को पायथन आवश्यकतानुसार छोटा या बड़ा कर सकता है। लेकिन step != 1 वाले एक्सटेंडेड स्लाइस बिखरे हुए इंडेक्स चुनते हैं, इसलिए इनमें लंबाई बिल्कुल बराबर होना अनिवार्य है।

Related Posts

  • Loading related posts...

Post a Comment

Search Topic Notes & 20 MCQs (Hindi / English)