PGDCA, BCA, B-Tech, MCA, M-Tech, B.Sc computer science राजस्थान सूचना सहायक व कंप्यूटर शिक्षक विशेष नोट्स: इस अध्याय में हम Python Lists: Creation, Characteristics (Ordered, Mutable, Heterogeneous) (पायथन लिस्ट्स: ऑर्डर्ड, म्यूटेबल और विभिन्न डेटा प्रकारों का संग्रह) की संपूर्ण थ्योरी, मुख्य तकनीकी बिंदु, तुलनात्मक तालिका और विगत परीक्षाओं के पैटर्न पर आधारित 20 अति-महत्वपूर्ण बहुविकल्पीय प्रश्न (MCQs) विस्तृत हिंदी व अंग्रेजी व्याख्या सहित पढ़ेंगे।
Overview: A Python list is an ordered, mutable, dynamic-array sequence capable of storing heterogeneous objects and duplicates, created with square brackets [...] or list(iterable). While indexing and amortized append() run in O(1) time, lists are unhashable, and multiplying nested lists ([[0]*3]*2) shares inner references rather than creating independent rows.
अध्याय सार: पायथन लिस्ट एक ऑर्डर्ड (ordered), म्यूटेबल (mutable) और डायनामिक-एरे आधारित सीक्वेंस है जो विभिन्न डेटा प्रकारों (heterogeneous) और डुप्लिकेट मानों को रख सकती है। इसमें इंडेक्सिंग और append() O(1) समय में होते हैं, लेकिन म्यूटेबल होने के कारण लिस्ट अनहैशेबल होती है और [[0]*3]*2 से अंदरूनी रेफरेंस साझा हो जाते हैं।
Python Lists: Creation, Characteristics (Ordered, Mutable, Heterogeneous): Detailed Notes & Architecture (पायथन लिस्ट्स: ऑर्डर्ड, म्यूटेबल और विभिन्न डेटा प्रकारों का संग्रह)
1. List Creation Syntax and the Nested Repetition Trap (लिस्ट निर्माण के तरीके और नेस्टेड रेपिटिशन की त्रुटि)
[EN] Lists are constructed using bracket literals ([10, 'AI', True]), empty [] or list(), the list(iterable) constructor (e.g., list('PY') -> ['P', 'Y'], list(range(3)) -> [0, 1, 2]), or list comprehensions ([x * x for x in range(4)]). Lists support concatenation (+) and repetition (*), e.g., [1, 2] * 3 -> [1, 2, 1, 2, 1, 2]. Classic exam trap: creating a 2D grid via grid = [[0] * 3] * 2 duplicates the reference to the SAME inner list object (grid[0] is grid[1] is True), so modifying grid[0][0] = 9 alters every row; always use [[0] * 3 for _ in range(2)] instead.
[HI] लिस्ट का निर्माण [] लिटरल, list(iterable) कन्स्ट्रक्टर (जैसे list('PY') -> ['P', 'Y']), या लिस्ट कॉम्प्रिहेंशन से होता है। लिस्ट + (जोड़) और * (दोहराव) ऑपरेटर्स का समर्थन करती है। परीक्षा का प्रमुख ट्रैप: [[0] * 3] * 2 लिखने पर दोनों पंक्तियाँ एक ही अंदरूनी लिस्ट का रेफरेंस साझा करती हैं (grid[0] is grid[1] सत्य), इसलिए एक को बदलने पर दूसरी भी बदल जाती है; इसके बजाय [[0] * 3 for _ in range(2)] लिखें।
2. Core Characteristics: Ordered, Mutable, Heterogeneous, Duplicate-Friendly (मुख्य विशेषताएँ: ऑर्डर्ड, म्यूटेबल, विषम और डुप्लिकेट समर्थित)
[EN] First, lists are Ordered: elements preserve positional sequence and support 0-based positive/negative indexing and slicing (Note: two lists with the same items in different order are NOT equal: [1, 2] != [2, 1]). Second, lists are Mutable: elements can be updated in-place (lst[1] = 99), slices replaced with different lengths (lst[1:2] = [10, 20, 30]), and items deleted via del lst[0]; mutability also makes lists unhashable (cannot be dict keys or set elements). Third, lists are Heterogeneous and allow duplicates: a single list can hold ints, strings, floats, functions, and nested lists simultaneously, including repeated values.
[HI] पहला, लिस्ट ऑर्डर्ड होती हैं: तत्व अपना निश्चित स्थान बनाए रखते हैं और इंडेक्सिंग/स्लाइसिंग सपोर्ट करते हैं (अतः [1, 2] != [2, 1])। दूसरा, लिस्ट म्यूटेबल होती हैं: तत्वों को इन-प्लेस बदला (lst[1] = 99), स्लाइस द्वारा आकार बदला और del से हटाया जा सकता है; म्यूटेबल होने से लिस्ट अनहैशेबल होती है। तीसरा, लिस्ट हेटेरोजीनियस होती हैं और डुप्लिकेट मान रख सकती हैं—एक ही लिस्ट में संख्या, स्ट्रिंग, बूलियन व नेस्टेड लिस्ट साथ रह सकती हैं।
3. Internal Architecture: Dynamic Array of Object Pointers (आंतरिक संरचना: ऑब्जेक्ट पॉइंटर्स का डायनामिक एरे)
[EN] Contrary to a common misconception, CPython implements list as a contiguous dynamic array of PyObject* pointers—NOT as a linked list. That is why a Python list easily stores heterogeneous types (every slot simply stores a fixed-size memory pointer to any Python object) and why random indexing lst[i] and len(lst) execute in O(1) time. CPython over-allocates extra buffer capacity so appending at the end (append()) is amortized O(1), whereas inserting or popping at the front (insert(0, x) or pop(0)) shifts all remaining pointers in O(n) time.
[HI] एक सामान्य भ्रम के विपरीत, CPython में list कोई लिंक्ड लिस्ट नहीं बल्कि ऑब्जेक्ट पॉइंटर्स (PyObject*) का एक सतत डायनामिक एरे होती है। यही कारण है कि एक लिस्ट अलग-अलग प्रकार के डेटा (heterogeneous) आसानी से रखती है तथा lst[i] और len(lst) की समय जटिलता O(1) होती है। अंत में जोड़ना (append) अमॉर्टाइज्ड O(1) है, जबकि शुरुआत में insert(0, x) या pop(0) करने में सभी पॉइंटर्स खिसकाने के कारण O(n) समय लगता है।
4. Aliasing vs Shallow Copy vs Deep Copy (एलियासिंग बनाम शैलो कॉपी बनाम डीप कॉपी)
[EN] Assigning b = a does not duplicate the list; both names point to the exact same list object (a is b is True). Calling b = a.copy(), b = a[:], or b = list(a) creates a shallow copy: a brand-new outer list (a is not b) whose nested mutable elements (like sublists) still share references with a. To make nested sublists completely independent, import Python's copy module and use copy.deepcopy(a).
[HI] b = a लिखने से लिस्ट की कॉपी नहीं बनती बल्कि दोनों वेरिएबल एक ही लिस्ट (एलियास) की ओर संकेत करते हैं। a.copy(), a[:] या list(a) से शैलो कॉपी बनती है, जिसमें बाहरी लिस्ट तो नई होती है लेकिन उसके भीतर की नेस्टेड लिस्ट पुराने रेफरेंस ही साझा करती हैं। नेस्टेड म्यूटेबल तत्वों को भी पूरी तरह स्वतंत्र कॉपी करने के लिए copy.deepcopy(a) का प्रयोग किया जाता है।
Syntax, Formula & Technical Example (Python Lists: Creation, Characteristics (Ordered, Mutable, Heterogeneous))
mixed = [10, 'AI', 3.14, [1, 2], 10]
mixed[1] = 'ML'
print(mixed[0], mixed[-2][1]) # 10 2
print(list('PY')) # ['P', 'Y']
a = [[0] * 2] * 2
a[0][0] = 7
print(a) # [[7, 0], [7, 0]]
b = mixed.copy()
print(b == mixed, b is mixed) # True False
Python Lists: Creation, Characteristics (Ordered, Mutable, Heterogeneous) Quick Reference & Comparison Table
| Property | Python List Behavior | Key Exam Fact |
|---|---|---|
| Ordering | Ordered sequence (0-based index) | [1, 2] == [2, 1] evaluates to False |
| Mutability | Mutable in-place (unhashable) | Cannot be dict key or set member |
| Data Types | Heterogeneous & allows duplicates | Stores pointers to mixed object types |
| Implementation | Contiguous dynamic pointer array | O(1) indexing; O(n) insert(0)/pop(0) |
Python Lists: Creation, Characteristics (Ordered, Mutable, Heterogeneous) Top 20 Bilingual MCQs with Answers
Q1. How is a Python list internally implemented in CPython?
(CPython में पायथन लिस्ट को आंतरिक रूप से कैसे इम्प्लीमेंट किया जाता है?)
(A) A hash table indexed by integer keys
(B) A contiguous dynamic array of pointers to Python objects
(C) A doubly linked list of nodes
(D) A singly linked list of fixed-type values
Correct Answer: (B)
[EN] CPython's list is an over-allocated dynamic array of PyObject* pointers, giving O(1) random indexing and heterogeneous storage.
[HI] CPython में list ऑब्जेक्ट पॉइंटर्स का डायनामिक एरे होती है, जिससे O(1) इंडेक्सिंग और विषम डेटा संग्रह संभव होता है।
Q2. What is the output of: x = [[0] * 2] * 3; x[1][0] = 9; print(x)?
(x = [[0] * 2] * 3; x[1][0] = 9; print(x) का आउटपुट क्या होगा?)
(A) [[0, 0], [9, 0], [0, 0]]
(B) [[9, 0], [9, 0], [9, 0]]
(C) [[9, 9], [9, 9], [9, 9]]
(D) TypeError
Correct Answer: (B)
[EN] [[0] * 2] * 3 replicates 3 references to the same inner list, so mutating x[1][0] reflects across all 3 rows.
[HI] [[0] * 2] * 3 में तीनों पंक्तियाँ एक ही अंदरूनी लिस्ट की ओर संकेत करती हैं, इसलिए एक पंक्ति बदलने पर तीनों बदल जाती हैं।
Q3. What is the output of: print(list('ABC'))?
(print(list('ABC')) का आउटपुट क्या होगा?)
(A) TypeError
(B) ('A', 'B', 'C')
(C) ['A', 'B', 'C']
(D) ['ABC']
Correct Answer: (C)
[EN] The list(iterable) constructor iterates over 'ABC' character by character and builds ['A', 'B', 'C'].
[HI] list(iterable) कन्स्ट्रक्टर 'ABC' के प्रत्येक अक्षर पर इटरेट करके ['A', 'B', 'C'] बनाता है।
Q4. What happens when you try to use a list as a dictionary key, e.g., d = {[1, 2]: 'a'}?
(जब आप लिस्ट को डिक्शनरी की कुंजी (key) बनाते हैं जैसे d = {[1, 2]: 'a'}, तो क्या होता है?)
(A) Raises TypeError: unhashable type: 'list'
(B) Raises KeyError
(C) Creates the dictionary cleanly
(D) Converts the list to a tuple automatically
Correct Answer: (A)
[EN] Because lists are mutable, Python explicitly sets __hash__ = None on list, making them unhashable for dict keys or sets.
[HI] लिस्ट म्यूटेबल होने के कारण अनहैशेबल होती है, इसलिए इसे डिक्शनरी कुंजी या सेट में रखने पर TypeError आता है।
Q5. What is the output of: a = [1, 2, 3, 4]; a[1:3] = [10, 20, 30]; print(a)?
(a = [1, 2, 3, 4]; a[1:3] = [10, 20, 30]; print(a) का आउटपुट क्या होगा?)
(A) [1, [10, 20, 30], 4]
(B) [1, 10, 20, 30, 4]
(C) [1, 10, 20, 4]
(D) Raises ValueError
Correct Answer: (B)
[EN] Slice assignment replaces elements at indices 1 and 2 ([2, 3]) with the unpacked elements of [10, 20, 30], resizing the list in-place.
[HI] स्लाइस असाइनमेंट इंडेक्स 1 व 2 के तत्वों को [10, 20, 30] के तीन तत्वों से बदलकर लिस्ट का आकार इन-प्लेस बढ़ा देता है।
Q6. What is the time complexity of accessing lst[i] and calling lst.insert(0, x) on a list of size n?
(n आकार की लिस्ट पर lst[i] और lst.insert(0, x) की समय जटिलता क्रमशः क्या है?)
(A) O(n) and O(1)
(B) O(log n) and O(n)
(C) O(1) and O(n)
(D) O(1) and O(1)
Correct Answer: (C)
[EN] Dynamic array indexing lst[i] takes O(1) direct offset lookup, whereas insert(0, x) must shift all n pointers right in O(n).
[HI] डायनामिक एरे में lst[i] सीधे O(1) समय में मिलता है, जबकि insert(0, x) के लिए सभी n तत्व खिसकाने में O(n) समय लगता है।
Q7. What is the output of: a = [1, [2, 3]]; b = a.copy(); b[1][0] = 99; print(a)?
(a = [1, [2, 3]]; b = a.copy(); b[1][0] = 99; print(a) का आउटपुट क्या होगा?)
(A) [99, [2, 3]]
(B) [1, [99, 3]]
(C) [1, [2, 3]]
(D) TypeError
Correct Answer: (B)
[EN] a.copy() performs a shallow copy: the outer list is new, but the nested sublist [2, 3] is still shared between a and b.
[HI] a.copy() शैलो कॉपी बनाता है: बाहरी लिस्ट नई है पर अंदरूनी सब-लिस्ट [2, 3] दोनों में साझा होने के कारण a भी बदल जाता है।
Q8. What does [1, 2, 3] == [1, 3, 2] evaluate to in Python?
(पायथन में [1, 2, 3] == [1, 3, 2] का परिणाम क्या होता है?)
(A) True
(B) TypeError
(C) False
(D) None
Correct Answer: (C)
[EN] Lists are ordered sequences, so equality checks require identical elements in the exact same positional order.
[HI] लिस्ट एक ऑर्डर्ड सीक्वेंस है, इसलिए == तभी True होगा जब दोनों में तत्व ठीक उसी क्रम में हों।
Q9. What is the output of: x = [10, 20, 30]; y = x; y[0] = 5; print(x[0])?
(x = [10, 20, 30]; y = x; y[0] = 5; print(x[0]) का आउटपुट क्या होगा?)
(A) 10
(B) 30
(C) TypeError
(D) 5
Correct Answer: (D)
[EN] y = x creates an alias referencing the exact same mutable list object, so changing y[0] mutates x[0].
[HI] y = x से कोई कॉपी नहीं बनती बल्कि दोनों एक ही लिस्ट को पॉइंट करते हैं, अतः y[0] बदलने से x[0] भी 5 हो जाता है।
Q10. Which expression creates an empty list in Python?
(पायथन में कौन सा व्यंजक एक खाली (empty) लिस्ट बनाता है?)
(A) list(0)
(B) {}
(C) ()
(D) list()
Correct Answer: (D)
[EN] Both [] and list() create an empty list; {} creates an empty dict, () creates an empty tuple, and list(0) raises TypeError (int not iterable).
[HI] [] और list() खाली लिस्ट बनाते हैं; {} खाली dict, () खाली टपल बनाता है और list(0) से TypeError आता है।
Q11. What happens when you execute list(123)?
(list(123) चलाने पर क्या होता है?)
(A) Returns [1, 2, 3]
(B) Returns []
(C) Returns [123]
(D) Raises TypeError: 'int' object is not iterable
Correct Answer: (D)
[EN] The list() constructor requires an iterable argument such as a string, tuple, set, or range, not an integer.
[HI] list() कन्स्ट्रक्टर को एक इटरेबल ऑब्जेक्ट (जैसे str, tuple, range) चाहिए होता है; int देने पर TypeError आता है।
Q12. How do you safely create a 3x2 list of zeros with completely independent inner rows?
(पूरी तरह स्वतंत्र पंक्तियों वाली 3x2 शून्य (zeros) लिस्ट बनाने का सही तरीका क्या है?)
(A) [[0] * 2] * 3
(B) [[0] * 2 for _ in range(3)]
(C) [0] * 2 * 3
(D) list([0] * 2) * 3
Correct Answer: (B)
[EN] A list comprehension evaluates [0] * 2 freshly on each of the 3 iterations, creating 3 distinct inner list objects.
[HI] लिस्ट कॉम्प्रिहेंशन हर इटरेशन में एक नई स्वतंत्र अंदरूनी लिस्ट [0] * 2 बनाता है।
Q13. What is the output of: lst = [1, 2, 3, 4]; del lst[1:3]; print(lst)?
(lst = [1, 2, 3, 4]; del lst[1:3]; print(lst) का आउटपुट क्या होगा?)
(A) [1, 4]
(B) [1]
(C) [2, 3]
(D) [1, 3, 4]
Correct Answer: (A)
[EN] del lst[1:3] removes the slice at indices 1 and 2 (elements 2 and 3) from the list in-place.
[HI] del lst[1:3] इंडेक्स 1 और 2 के तत्वों (2 व 3) को लिस्ट से इन-प्लेस हटा देता है, जिससे [1, 4] बचता है।
Q14. What is the output of: print(bool([False]))?
(print(bool([False])) का आउटपुट क्या होगा?)
(A) TypeError
(B) None
(C) False
(D) True
Correct Answer: (D)
[EN] Any non-empty list is truthy in Python regardless of what value its element holds (len([False]) == 1).
[HI] कोई भी गैर-खाली लिस्ट ट्रूथी होती है, चाहे उसके अंदर का तत्व False ही क्यों न हो (क्योंकि उसकी लंबाई 1 है)।
Q15. Which function creates a true deep copy of a nested list so inner sublists do not share references?
(नेस्टेड लिस्ट की ऐसी पूर्ण कॉपी किस फंक्शन से बनती है कि अंदरूनी सब-लिस्ट भी अलग रहें?)
(A) copy.deepcopy()
(B) list(lst)
(C) list.copy()
(D) lst[:]
Correct Answer: (A)
[EN] copy(), lst[:], and list(lst) all create shallow copies; only copy.deepcopy() recursively duplicates nested mutable objects.
[HI] copy(), lst[:] और list(lst) केवल शैलो कॉपी बनाते हैं; केवल copy.deepcopy() ही अंदरूनी ऑब्जेक्ट्स की भी नई कॉपी बनाता है।
Q16. Which of the following statements about Python lists is NOT true?
(पायथन लिस्ट्स के बारे में निम्नलिखित में से कौन सा कथन सत्य नहीं है?)
(A) Lists are mutable and support in-place item and slice assignment
(B) len(my_list) runs in O(1) constant time in CPython
(C) All elements inside a single Python list must share the same data type
(D) Lists maintain insertion/positional order and allow duplicate elements
Correct Answer: (C)
[EN] Python lists are heterogeneous (unlike typed C arrays or array.array) and freely store mixed data types together.
[HI] पायथन लिस्ट हेटेरोजीनियस होती है—इसमें एक ही समय में अलग-अलग डेटा प्रकार के तत्व आसानी से रखे जा सकते हैं।
Q17. Within Python Lists: Creation, Characteristics (Ordered, Mutable, Heterogeneous), which property directly maps to: "Ordered sequence (0-based index)"?
(Python Lists: Creation, Characteristics (Ordered, Mutable, Heterogeneous) में कौन सा Property सीधे "Ordered sequence (0-based index)" से संबंधित है?)
(A) Ordering
(B) Mutability
(C) Data Types
(D) Implementation
Correct Answer: (A)
[EN] In Python Lists: Creation, Characteristics (Ordered, Mutable, Heterogeneous), 'Ordering' handles 'Ordered sequence (0-based index)' ([1, 2] == [2, 1] evaluates to False).
[HI] Python Lists: Creation, Characteristics (Ordered, Mutable, Heterogeneous) में 'Ordering' का कार्य/विवरण 'Ordered sequence (0-based index)' ([1, 2] == [2, 1] evaluates to False) है।
Q18. Which option accurately describes 'Mutability' in Python Lists: Creation, Characteristics (Ordered, Mutable, Heterogeneous)?
(Python Lists: Creation, Characteristics (Ordered, Mutable, Heterogeneous) के संदर्भ में 'Mutability' की सही विशेषता कौन सी है?)
(A) Ordered sequence (0-based index)
(B) Contiguous dynamic pointer array
(C) Mutable in-place (unhashable)
(D) Heterogeneous & allows duplicates
Correct Answer: (C)
[EN] 'Mutability' provides 'Mutable in-place (unhashable)' — key point: Cannot be dict key or set member.
[HI] 'Mutability' का मुख्य विवरण 'Mutable in-place (unhashable)' है (Cannot be dict key or set member)।
Q19. In Python Lists: Creation, Characteristics (Ordered, Mutable, Heterogeneous), identify the correct parameter for the specification: "Heterogeneous & allows duplicates".
(Python Lists: Creation, Characteristics (Ordered, Mutable, Heterogeneous) में "Heterogeneous & allows duplicates" विवरण किस विकल्प के लिए सही है?)
(A) Implementation
(B) Ordering
(C) Data Types
(D) Mutability
Correct Answer: (C)
[EN] 'Data Types' corresponds to 'Heterogeneous & allows duplicates' (Stores pointers to mixed object types).
[HI] 'Data Types' का संबंध 'Heterogeneous & allows duplicates' (Stores pointers to mixed object types) से है।
Q20. Considering Python Lists: Creation, Characteristics (Ordered, Mutable, Heterogeneous), what key behavior defines 'Implementation'?
(Python Lists: Creation, Characteristics (Ordered, Mutable, Heterogeneous) में 'Implementation' की मुख्य भूमिका या मान क्या है?)
(A) Ordered sequence (0-based index)
(B) Heterogeneous & allows duplicates
(C) Contiguous dynamic pointer array
(D) Mutable in-place (unhashable)
Correct Answer: (C)
[EN] 'Implementation' represents 'Contiguous dynamic pointer array' (O(1) indexing; O(n) insert(0)/pop(0)).
[HI] 'Implementation' का अर्थ/मान 'Contiguous dynamic pointer array' है (O(1) indexing; O(n) insert(0)/pop(0))।
Python Lists: Creation, Characteristics (Ordered, Mutable, Heterogeneous) — Exam FAQs
Q1: How does a Python list differ from C's array or Python's array.array?
पायथन की list, C के एरे या Python के array.array से कैसे भिन्न है?
[EN] C arrays and array.array store homogeneous raw values contiguously in memory with fixed types, whereas a Python list stores PyObject* references to objects anywhere in memory, enabling mixed types and dynamic resizing.
[HI] C एरे और array.array मेमोरी में एक ही डेटा टाइप के कच्चे मान सीधे रखते हैं, जबकि पायथन list अलग-अलग प्रकार के ऑब्जेक्ट्स के पॉइंटर्स संग्रहीत करती है और आकार बदल सकती है।
Q2: Why can't I write a + 4 to append 4 to list a = [1, 2]?
लिस्ट a = [1, 2] में 4 जोड़ने के लिए a + 4 क्यों नहीं लिख सकते?
[EN] The + operator on lists only concatenates another list (a + [4]), whereas passing an integer raises TypeError. Use a.append(4) to add a single item in-place.
[HI] लिस्ट पर + ऑपरेटर केवल दूसरी लिस्ट (a + [4]) को जोड़ता है, पूर्णांक देने पर TypeError आता है। एक तत्व जोड़ने के लिए a.append(4) का उपयोग करें।
Q3: What is the quickest way to copy a flat (non-nested) list without aliasing?
एक सामान्य (गैर-नेस्टेड) लिस्ट की स्वतंत्र कॉपी बनाने का सबसे सरल तरीका क्या है?
[EN] Use a.copy(), a[:], or list(a). All three allocate a new list object with identical elements so mutations on the copy won't affect the original flat list.
[HI] a.copy(), a[:] या list(a) का प्रयोग करें; तीनों एक नई स्वतंत्र लिस्ट बनाते हैं जिससे कॉपी बदलने पर मूल लिस्ट प्रभावित नहीं होती।
Post a Comment
Post a Comment