Python में list, वस्तुओं का समुह है जिसे क्रम में रखा जाता है। यह numbers, words, या अन्य लिस्ट को भी रख सकता है। [] का इस्तेमाल करके list बनाई जाती है और items के बीच comma (,) लगाया जाता है।
उदाहरण के लिए
my_list = [10, "apple", 3.14, [1, 2]] एक लिस्ट है जिसमे अलग-अलग प्रकार के डेटा को रखा गया है।
प्रत्येक आइटम का एक नंबर होता है जिसे Index कहते हैं।
प्रत्येक आइटम का एक नंबर होता है जिसे Index कहते हैं।
इस Index से आइटम के स्थान का पता चलता है। जैसे ऊपर के उदाहरण में 10 के लिए Index 0 होगा, apple के लिए 1 और वैसे बाकि आइटम्स के लिए होगा।
List बनाने के तरीके (Creating a List)
आप square brackets `[]` के अंदर values डालकर और उन्हें comma से अलग-अलग करके list बना सकते हैं।Examples:
List of numbers
numbers = [10, 20, 30, 40, 50]
print(numbers)
Output: [10, 20, 30, 40, 50]
List of strings
fruits = ["apple", "orange", "mango", "pear"]
print(fruits)
Output: ['apple', 'orange', 'mango', 'pear']
Mixed data types in a list
data_variety = ["Sumit", 25, 5.6, True]
print(data_variety)
Output: ['Sumit', 25, 5.6, True]
Empty list
empty_list = []
print(empty_list)
Output: []
List created using the list() function
letters = list(("a", "b", "c", "d"))
print(letters)
Output: ['a', 'b', 'c', 'd']
Nested list (list inside another list)
nested = [["red", "blue"], [7, 8, 9]]
print(nested)
Output: [['red', 'blue'], [7, 8, 9]]
List में Items को Access करना
पायथन में list से एक आइटम प्राप्त करने के लिए उसके स्थान (position) नंबर का उपयोग किया जाता है जिसे Index number कहते है।पहले आइटम के लिए index number 0 से शुरू होता है, दूसरे के लिए 1 और इसी तरह आगे बढ़ता है।
list में से किसी item को प्राप्त करने के लिए square brackets में लिस्ट का नाम और उसके Index number का उपयोग किया जाता है।
Example:
fruits = ["apple", "banana", "mango", "orange"] print(fruits[0]) # apple (first item) print(fruits[1]) # banana (second item) print(fruits[2]) # mango (third item)
Output:
apple
banana
mango
Negative index लिस्ट, के अंत से शुरू होते हैं:
- -1 आखिरी आइटम होता है
- -2 आखिरी से दूसरा आइटम होता है और इसी तरह आगे
fruits = ["apple", "banana", "mango", "orange"] print(fruits[-1]) # orange (last item) print(fruits[-2]) # orange (second item from the end )
Output: orange mango
List में Items को Modify करना
Python, में lists को बदला जा सकता है। इसका मतलब है कि लिस्ट के निर्माण हो जाने के बाद आप उसके किसी भी आइटम को update या replace कर सकते हैं।Example:
उदाहरण के लिए हम दूसरे आइटम (index `1`) banana को बदलकर orange रखना चाहते हैं।fruits = ["apple", "banana", "mango"] fruits[1] = "orange" print(fruits) # ['apple', 'orange', 'mango'
Output: ['apple', 'orange', 'mango']
आप लिस्ट के अन्त से आइटम्स को बदलने के लिए negative indexes का उपयोग भी कर सकते हैं।
Example:
mango बदलकर grapes हो गया है।fruits = ["apple", "banana", "mango"] fruits[-1] = "grapes" print(fruits) # ['apple', 'orange', 'grapes']
Output: ['apple', 'banana', 'grapes']
Deleting List Items in Python in Hindi
Python में आप list से items को कई तरीकों को से डिलीट कर सकते हैं। list में परिवर्तन किया जा सकता है ,अतः आप आवश्यकता पड़ने पर items को हटा सकते हैं।`del` keyword का उपयोग करके
del keyword किसी list से किसी खास स्थान (index) का item हटाने के लिए इस्तेमाल होता है।numbers = [10, 20, 30, 40, 50] del numbers[2] # removes item at index 2 (30) print(numbers)
Output: [10, 20, 40, 50]
`remove()` method का उपयोग करके
remove() function list से पहला matching value हटाता है।fruits = ["apple", "banana", "cherry", "banana"] fruits.remove("banana") # removes first "banana" print(fruits)
Output: ['apple', 'cherry', 'banana']
`pop()` method
का उपयोग करके
pop() method list से index वाला item हटाकर वापस देता है।colors = ["red", "green", "blue"] removed_item = colors.pop(1) # removes "green" print(colors) print("Removed:", removed_item)
Output: ['red', 'blue'] Removed: green
अगर index नहीं देते, तो pop() आखिरी item हटाता है और उसे return करता है।
`clear()` method का उपयोग करके
`clear()` method, list से सभी items को delete कर देता है।data = [16, 24, 10,14] data.clear() print(data)
Output: []
अतः आप अपनी ज़रूरत के अनुसार list से item हटाने के लिए del, remove(), pop(), या clear() का इस्तेमाल कर सकते हैं।
del → index से item हटाता है।
remove() → value से item हटाता है।
pop() → index से item हटाता है और उसे वापस भी देता है।
clear() → पूरी list खाली कर देता है।
List में Loop चलाना (Iterating through List)
एक List में looping का मतलब list के प्रत्येक आइटम में एक के बाद एक जाना। यह उस समय उपयोगी है जब आप एक ही लाइन के code को बार-बार लिखें बिना लिस्ट के सभी items को print, change, या use करना चाहते हैं। हम इसके लिए अधिकांशत for loop का उपयोग करते हैं।Example 1 – Simple loop:
colors = ["red", "green", "blue"] for color in colors: print(color)
Output: red green blue
Example 2 – With index numbers:
colors = ["red", "green", "blue"] for i in range(len(colors)): print(i, colors[i])
Output: 0 red 1 green 2 blue
Example 3 – Changing values:
numbers = [1, 2, 3] for i in range(len(numbers)): numbers[i] = numbers[i] * 2 print(numbers)
Output: [2, 4, 6]
Nested List (List के अंदर List)
एक nested list, एक ऐसा list है जो अन्य lists को items की भांति रखता है। यह data को structured तरीके से, जैसे tables या groups में, store करता है। आप multiple indexes का उपयोग करके एक nested list में items को access कर सकते हैं।Example1
nested = [[1, 2], [3, 4], [5, 6]] # Accessing items print(nested[0]) print(nested[0][1]) print (nested[2][1])
Output: [1, 2] 2 6
ऊपर, `nested[0]` पहला inner list `[1, 2]` को देता है। `nested[0][1]` पहले inner list से दूसरे जो कि 2 है, को देता है।
उसी प्रकार यदि आपको 6 को access करना है तो nested[2][1] लिखना होगा
Example2:
nested = [["apple", "banana"], ["cat", "dog"], ["red", "blue"]] # Accessing items print(nested[0]) print(nested[0][1]) print(nested[2][0])
Output: ['apple', 'banana'] banana red
List के Common Methods
Python में List के Common Methods का उपयोग लिस्ट में तत्व जोड़ने, हटाने, खोजने और व्यवस्थित करने के लिए किया जाता है। ये methods निम्न है
append()
यह function list के अंत में नया item जोड़ता है।
Example:
numbers = [1, 2, 3] numbers.append(4) print(numbers)
Output: [1, 2, 3, 4]
insert():
यह function list में किसी विशेष index पर item डालता है।
Example:
numbers = [1, 2, 3] numbers.insert(1, 10) print(numbers)
Output: [1, 10, 2, 3]
remove():
यह function list से किसी item की पहली बार मिलने वाली value को हटाता है।
Example:
numbers = [10, 12, 30, 12] numbers.remove(2) print(numbers)
Output: [10, 30, 12]
pop():
यह function किसी index का item हटाकर वापस देता है।
Example:
numbers = [1, 2, 3] x = numbers.pop(1) print(x) print(numbers)
Output: 2 [1, 3]
sort():
यह function list के items को ascending order में arrange करता है।
Example:
numbers = [3, 1, 4, 2] numbers.sort() print(numbers)
Output: [1, 2, 3, 4]
reverse():
यह function list के items को उल्टे क्रम (reverse order) में कर देता है।
Example:
numbers = [10, 20, 30, 40] numbers.reverse() print(numbers)
Output: [40, 30, 20, 10]
List में Membership Test (in और not in)
Membership test, का उपयोग यह जांच करने के लिए किया जाता है कि क्या लिस्ट में आइटम उपस्थित है या नहीं। इसके लिए हम दो keywords का उपयोग करते हैं।in → यह जांचता है कि क्या कोई आइटम लिस्ट में उपस्थित है।
not in → यह जांचता है कि क्या कोई आइटम लिस्ट में उपस्थित नहीं है।
Example with `in`
numbers = [10, 20, 30, 40] print(20 in numbers) # True (20 is in the list) print(50 in numbers) # False (50 is not in the list)
Output: True False
Example with `not in`
fruits = ["apple", "banana", "mango"] print("apple" not in fruits) # False (apple is in the list) print("orange" not in fruits) # True (orange is not in the list
Output: False True
Advantages and limitations of lists in Python in Hindi
Advantages of List in Hindi
List के निम्न में फायदे हैं।1. Lists, एक वेरिएबल में कई आइटम्स को स्टोर कर सकता है।
2. एक list में विभिन्न प्रकार की डाटा टाइप जैसे integers, strings, floats, वाले data को स्टोर करने की सुविधा देता ।
3. लिस्ट परिवर्तनशील होते है इसलिए items को आसानी से बदला, जोड़ा या हटाया जा सकता है।
4. Lists, आसानी से access के लिए indexing और slicing को support करता है।
5. कई built-in methods (append, insert, sort, आदि.) इसे शक्तिशाली बनाता है।
6. ये nested हो सकते है। (एक list के अंदर list)
Limitations of List in Hindi(लिस्ट की सीमाएं)
1. Lists, arrays की अपेक्षा ज्यादा memory लेता है।2. बहुत बड़ी lists के लिए item को खोजना धीमी हो सकता है।
3. मध्य में से items को Insert या delete करना समय लेने वाला हो सकता है।
4. Lists का कोई fixed type नहीं होता, इसलिए गलती (error) हो सकती है।
5. Loop में चलाने पर Lists, tuple से धीमी होती हैं।
6. Numbers के काम में Lists, NumPy arrays से कमजोर और धीमी होती हैं।
0 टिप्पणियाँ