इस लेख में, हम पायथन सूचियों के बारे में सब कुछ सीखेंगे, वे कैसे बनाई जाती हैं, सूची का टुकड़ा करना, उनसे तत्वों को जोड़ना या निकालना और इसी तरह।
वीडियो: पायथन लिस्ट और टुपल्स
पायथन कई प्रकार के यौगिक डेटा प्रकार प्रदान करता है जिन्हें अक्सर अनुक्रम के रूप में संदर्भित किया जाता है। सूची पायथन में प्रयुक्त सबसे अक्सर उपयोग किए जाने वाले और बहुत बहुमुखी डेटा प्रकारों में से एक है।
सूची कैसे बनाएं?
पायथन प्रोग्रामिंग में, वर्ग कोष्ठक के अंदर सभी वस्तुओं (तत्वों) को रखकर एक सूची बनाई जाती है ()
, जिसे कॉमा द्वारा अलग किया जाता है।
इसमें किसी भी संख्या में आइटम हो सकते हैं और वे विभिन्न प्रकार (पूर्णांक, फ्लोट, स्ट्रिंग आदि) के हो सकते हैं।
# empty list my_list = () # list of integers my_list = (1, 2, 3) # list with mixed data types my_list = (1, "Hello", 3.4)
एक सूची में आइटम के रूप में एक और सूची भी हो सकती है। इसे नेस्टेड लिस्ट कहा जाता है।
# nested list my_list = ("mouse", (8, 4, 6), ('a'))
सूची से तत्वों का उपयोग कैसे करें?
ऐसे कई तरीके हैं जिनसे हम किसी सूची के तत्वों तक पहुँच सकते हैं।
सूची सूचकांक
()
किसी सूची में किसी आइटम तक पहुंचने के लिए हम इंडेक्स ऑपरेटर का उपयोग कर सकते हैं । पायथन में, सूचकांक 0. पर शुरू होते हैं। इसलिए, 5 तत्वों वाली एक सूची में 0 से 4 तक का सूचकांक होगा।
इनके अलावा अन्य अनुक्रमणिकाओं को एक्सेस करने की कोशिश एक बढ़ाएगी IndexError
। सूचकांक एक पूर्णांक होना चाहिए। हम फ्लोट या अन्य प्रकारों का उपयोग नहीं कर सकते, इसके परिणामस्वरूप होगा TypeError
।
नेस्टेड इंडेक्सिंग का उपयोग करके नेस्टेड सूचियों को एक्सेस किया जाता है।
# List indexing my_list = ('p', 'r', 'o', 'b', 'e') # Output: p print(my_list(0)) # Output: o print(my_list(2)) # Output: e print(my_list(4)) # Nested List n_list = ("Happy", (2, 0, 1, 5)) # Nested indexing print(n_list(0)(1)) print(n_list(1)(3)) # Error! Only integer can be used for indexing print(my_list(4.0))
आउटपुट
poea 5 ट्रेसेबैक (सबसे हालिया कॉल अंतिम): फ़ाइल "", लाइन 21, TypeError में: सूची सूचकांकों को पूर्णांक या स्लाइस होना चाहिए, फ्लोट नहीं।
नकारात्मक अनुक्रमण
पायथन अपने अनुक्रमों के लिए नकारात्मक अनुक्रमण की अनुमति देता है। -1 का सूचकांक अंतिम आइटम को संदर्भित करता है, -2 को दूसरे अंतिम आइटम और इसी तरह।
# Negative indexing in lists my_list = ('p','r','o','b','e') print(my_list(-1)) print(my_list(-5))
जब हम उपरोक्त कार्यक्रम चलाते हैं, तो हमें निम्न आउटपुट मिलेगा:
ep

पायथन में सूचियों का टुकड़ा कैसे करें?
हम स्लाइसिंग ऑपरेटर :
(कोलन) का उपयोग करके एक सूची में कई मदों तक पहुँच सकते हैं ।
# List slicing in Python my_list = ('p','r','o','g','r','a','m','i','z') # elements 3rd to 5th print(my_list(2:5)) # elements beginning to 4th print(my_list(:-5)) # elements 6th to end print(my_list(5:)) # elements beginning to end print(my_list(:))
आउटपुट
('ओ', 'जी', 'आर') ('पी', 'आर', 'ओ', 'जी') ('ए', 'एम', 'आई', 'जेड') ('पी') ',' आर ',' ओ ',' जी ',' आर ',' ए ',' एम ',' आई ',' जेड ')
नीचे दिखाए गए तत्वों के बीच अनुक्रमणिका पर विचार करके स्लाइसिंग को सर्वश्रेष्ठ रूप से देखा जा सकता है। इसलिए यदि हम एक सीमा तक पहुंचना चाहते हैं, तो हमें दो सूचकांकों की आवश्यकता है जो सूची से उस हिस्से को काट देंगे।

तत्वों को सूची में कैसे बदलें या जोड़ें?
सूचियाँ परिवर्तनशील हैं, जिसका अर्थ है कि उनके तत्वों को स्ट्रिंग या ट्यूपल के विपरीत बदला जा सकता है।
हम =
एक आइटम या आइटम की एक श्रृंखला को बदलने के लिए असाइनमेंट ऑपरेटर ( ) का उपयोग कर सकते हैं ।
# Correcting mistake values in a list odd = (2, 4, 6, 8) # change the 1st item odd(0) = 1 print(odd) # change 2nd to 4th items odd(1:4) = (3, 5, 7) print(odd)
आउटपुट
(1, 4, 6, 8) (1, 3, 5, 7)
हम append()
विधि का उपयोग करके एक सूची में एक आइटम जोड़ सकते हैं या विधि का उपयोग करके कई आइटम जोड़ सकते हैं extend()
।
# Appending and Extending lists in Python odd = (1, 3, 5) odd.append(7) print(odd) odd.extend((9, 11, 13)) print(odd)
आउटपुट
(1, 3, 5, 7) (1, 3, 5, 7, 9, 11, 13)
हम +
दो सूचियों के संयोजन के लिए ऑपरेटर का भी उपयोग कर सकते हैं । इसे संघनन भी कहा जाता है।
*
ऑपरेटर समय की दी गई संख्या के लिए एक सूची को दोहराता है।
# Concatenating and repeating lists odd = (1, 3, 5) print(odd + (9, 7, 5)) print(("re") * 3)
आउटपुट
(1, 3, 5, 9, 7, 5) ('पुनः', 'पुनः', 'पुनः')
Furthermore, we can insert one item at a desired location by using the method insert()
or insert multiple items by squeezing it into an empty slice of a list.
# Demonstration of list insert() method odd = (1, 9) odd.insert(1,3) print(odd) odd(2:2) = (5, 7) print(odd)
Output
(1, 3, 9) (1, 3, 5, 7, 9)
How to delete or remove elements from a list?
We can delete one or more items from a list using the keyword del
. It can even delete the list entirely.
# Deleting list items my_list = ('p', 'r', 'o', 'b', 'l', 'e', 'm') # delete one item del my_list(2) print(my_list) # delete multiple items del my_list(1:5) print(my_list) # delete entire list del my_list # Error: List not defined print(my_list)
Output
('p', 'r', 'b', 'l', 'e', 'm') ('p', 'm') Traceback (most recent call last): File "", line 18, in NameError: name 'my_list' is not defined
We can use remove()
method to remove the given item or pop()
method to remove an item at the given index.
The pop()
method removes and returns the last item if the index is not provided. This helps us implement lists as stacks (first in, last out data structure).
We can also use the clear()
method to empty a list.
my_list = ('p','r','o','b','l','e','m') my_list.remove('p') # Output: ('r', 'o', 'b', 'l', 'e', 'm') print(my_list) # Output: 'o' print(my_list.pop(1)) # Output: ('r', 'b', 'l', 'e', 'm') print(my_list) # Output: 'm' print(my_list.pop()) # Output: ('r', 'b', 'l', 'e') print(my_list) my_list.clear() # Output: () print(my_list)
Output
('r', 'o', 'b', 'l', 'e', 'm') o ('r', 'b', 'l', 'e', 'm') m ('r', 'b', 'l', 'e') ()
Finally, we can also delete items in a list by assigning an empty list to a slice of elements.
>>> my_list = ('p','r','o','b','l','e','m') >>> my_list(2:3) = () >>> my_list ('p', 'r', 'b', 'l', 'e', 'm') >>> my_list(2:5) = () >>> my_list ('p', 'r', 'm')
Python List Methods
Methods that are available with list objects in Python programming are tabulated below.
They are accessed as list.method()
. Some of the methods have already been used above.
Python List Methods |
---|
append() - Add an element to the end of the list |
extend() - Add all elements of a list to the another list |
insert() - Insert an item at the defined index |
remove() - Removes an item from the list |
pop() - Removes and returns an element at the given index |
clear() - Removes all items from the list |
index() - Returns the index of the first matched item |
count() - Returns the count of the number of items passed as an argument |
sort() - Sort items in a list in ascending order |
reverse() - Reverse the order of items in the list |
copy() - Returns a shallow copy of the list |
Some examples of Python list methods:
# Python list methods my_list = (3, 8, 1, 6, 0, 8, 4) # Output: 1 print(my_list.index(8)) # Output: 2 print(my_list.count(8)) my_list.sort() # Output: (0, 1, 3, 4, 6, 8, 8) print(my_list) my_list.reverse() # Output: (8, 8, 6, 4, 3, 1, 0) print(my_list)
Output
1 2 (0, 1, 3, 4, 6, 8, 8) (8, 8, 6, 4, 3, 1, 0)
List Comprehension: Elegant way to create new List
List comprehension is an elegant and concise way to create a new list from an existing list in Python.
A list comprehension consists of an expression followed by for statement inside square brackets.
Here is an example to make a list with each item being increasing power of 2.
pow2 = (2 ** x for x in range(10)) print(pow2)
Output
(1, 2, 4, 8, 16, 32, 64, 128, 256, 512)
This code is equivalent to:
pow2 = () for x in range(10): pow2.append(2 ** x)
A list comprehension can optionally contain more for
or if statements. An optional if
statement can filter out items for the new list. Here are some examples.
>>> pow2 = (2 ** x for x in range(10) if x> 5) >>> pow2 (64, 128, 256, 512) >>> odd = (x for x in range(20) if x % 2 == 1) >>> odd (1, 3, 5, 7, 9, 11, 13, 15, 17, 19) >>> (x+y for x in ('Python ','C ') for y in ('Language','Programming')) ('Python Language', 'Python Programming', 'C Language', 'C Programming')
Other List Operations in Python
List Membership Test
We can test if an item exists in a list or not, using the keyword in
.
my_list = ('p', 'r', 'o', 'b', 'l', 'e', 'm') # Output: True print('p' in my_list) # Output: False print('a' in my_list) # Output: True print('c' not in my_list)
Output
True False True
Iterating Through a List
Using a for
loop we can iterate through each item in a list.
for fruit in ('apple','banana','mango'): print("I like",fruit)
Output
मुझे सेब पसंद है मुझे केला पसंद है मुझे आम पसंद है