Posted By Naveen Singh on 13 Mar 2023
python-program
List एक non-primitive data type होता है। जिसका प्रयोग हम data elements के collection को स्टोर करने के लिएकरते हैं। List में हम data elements को comma(,) से separate करते है।
Python में list को हम square [] bracket से denote करते है।
Example-:
fruit = ["apple", "banana", "cherry"]
print(fruit)
Output -:
["apple", "banana", "cherry"]
Python में list ordered, changeable, और duplicate value allow करता है|
Ordered-: ordered का मतलब है कि list में sare elements एक defined ordered में होते है और वो order change नहीं होगा।
Changeable-: इसका मतलब है कि एक बार list create हो जाने पर हम उस list के elements को change, add या remove कर सकते है।
Allow duplicates-: allow duplicate का मतलब है कि किसी भी element को हम same name से दोबारा बना सकते है।
Python में list को access कैसे करे? (how to access list in python?)
Python में list हम उसके index number के through access कर सकते है।
Example-:
Sport = ["cricket", "chess", "football"]
print(Sport[1])
Output-:
chess
Note-: python में list की indexing हमेशा 0 से start होती है।
Negative Indexing-: negative indexing का मतलब है की last index number से elements कि indexing करना,
जिसमे पहला element -1 index पे और दूसरा element -2 index पर होगा।
Example-:
fruit = ["apple", "banana", "cherry"]
print(fruit[-1])
Output-:
cherry
Range of indexes-: list में हम elements को हम उसकी range के through भी access कर सकते है। जिसे हम colon(:) से separate करते है।
Syntax-:
[Start:Step:Stop]
Example-:
fruit = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(fruit[2:5])
जिसमे index 2 include होगा पर index 5 include नही होगा. जिसका output कुछ इस प्रकार का होगा।
Output-:
["cherry","orange","kiwi"]
Range of Negative Indexes-: list में हम elements को उसकी range of negative indexing के through भी access कर सकते है|
Example-:
student= ["aman", "shivam", "soumya", "amit", "ajay", "rohit", "devendra"]
print(student[-2:-5])
Output-:
["rohit", "ajay", "amit"]
Python में list elements को हम index number के through change कर सकते हैं|
Example-:
fruit = ["apple", "banana", "cherry"]
fruit[1] = "blackcurrant"
print(fruit)
Output-:
['apple', 'blackcurrant', 'cherry']
Change a range of item values-: Python में हम elements को उसकी range के through भी change
कर सकते हैं| जिसके लिए हमें एक new list बनानी होगी और उसमें new value देनी होगी| और हम new list को उस index number की range से attach कर देंगे|
Example-:
fruitname = ["apple", "banana", "cherry", "orange", "kiwi", "mango"]
fruitname[1:3] = ["blackcurrant", "watermelon"]
print(fruitname)
Output-:
["apple", "blackcurrant", "watermelon", "orange", "kiwi", "mango"]
Note-: list की लंबाई बदल जाएगी जब number of insert element, number of replace elements से मेल नहीं खाती हैं|
यदि आप number of replace elements से कम element insert करते हैं, तो नए element आपके द्वारा specified स्थान पर insert किए जाएँगे और remaining elements उसी के accordingly चलेंगे|
Example-:
mylist = ["apple", "banana", "cherry"]
mylist[1:3] = ["watermelon"]
print(mylist)
Output-:
["apple","watermelon","cherry"]
list में हम elements को basically तीन तरह से add कर सकते हैं –
Append items-: append() function का use करके हम list के end में elements को add कर सकते हैं|
Example-:
mylist = ["apple", "banana", "cherry"]
mylist.append("orange")
print(mylist)
Output-:
['apple', 'banana', 'cherry', 'orange']
Insert items-: किसी specified index पर list element को insert करने के लिए, हम insert() function का उपयोग करते हैं|
Example-:
mylist = ["apple", "banana", "cherry"]
mylist.insert(1, "orange")
print(mylist)
Output-:
['apple', 'orange', 'banana', 'cherry']
Extend list-: इस list function का प्रयोग करके हम एक listको दूसरे list में add कर सकते हैं| Extend function का use basically दो list को add करने के लिए किया जाता है|
Example-:
mylist = ["apple", "banana", "cherry"]
thislist = ["mango", "pineapple", "papaya"]
mylist.extend(thislist)
print(mylist)
Output-:
['apple', 'banana', 'cherry', 'mango', 'pineapple', 'papaya']
list में हम elements को कई तरह से remove कर सकते हैं-
Remove()-: इस list function की help से हम किसी भी specified element को remove कर सकते हैं|
Example-:
mylist = ["apple", "banana", "cherry"]
mylist.remove("banana")
print(mylist)
Output-:
['apple', 'cherry']
Pop()-: इस list function की help से भी हम किसी भी specified element को remove कर सकते हैं|
Example-:
fruitname = ["apple", "banana", "cherry"]
fruitname.pop(1)
print(fruitname)
Output-:
[“apple”,”cherry”]
अगर आप कोई भी index value नहीं देते हो तो यह फंक्शन list के last element को remove कर देता है|
Example-:
mylist = ["apple", "banana", "cherry"]
mylist.pop()
print(mylist)
Output-:
['apple', 'banana']
del keyword-: del एक keyword होता है जिसका प्रयोग हम किसी भी index number पर element को remove करने के लिए करते हैं|
Example-:
mylist = ["apple", "banana", "cherry"]
del mylist[0]
print(mylist)
Output-:
['banana', 'cherry']
“del” keyword का प्रयोग हम पूरी list को remove करने के लिए भी कर सकते हैं|
Example-:
mylist = ["apple", "banana", "cherry"]
del mylist
Output-:
Traceback (most recent call last):
File "demo_list_del2.py", line 3, in <module>
print(mylist)#इससे error generate होगी क्योंकि आपने "इस list" को सफलतापूर्वक remove कर दिया है।
NameError: name 'mylist' is not defined
clear()-: यह function पूरी list को empty कर देता है|
Example-:
fruit = ["apple", "banana", "cherry"]
fruit.clear()
print(fruit)
Output-:
[]
Python में हम for loop की सहायता से list के elements को print कर सकते है।
Example-:
mylist = ["cricket", "chess", "hockey"]
for x in mylist:
print(x)
Output-:
cricket
chess
hockey
Loop Through Index Numbers-: loop में हम list elements को उसके index number के through भी access कर सकते है।
range() and len() इन दो function की मदद से हम list elements को उसके index number के through print कर सकते हैं।
Example-:
mylist = ["apple", "banana", "cherry"]
for i in range(len(mylist)):
print(mylist[i])
Output-:
apple
banana
cherry
Using a While Loop-:
python में list elements को हम while loop की सहायता से भी print कर सकते है।
जिसमे len() function का प्रयोग हम list की length बताने के लिए करते हैं।
Example-:
mylist = ["apple", "banana", "cherry"]
i = 0
while i < len(mylist):
print(mylist[i])
i = i + 1
Output-:
apple
banana
cherry
Python में list comprehension हमे short syntax provide करता है, जिसकी सहायता से हम existing list की values के आधार पर new list create कर सकते है।
Syntax-:
newlist = [expression for item in iterable if condition == True]
Condition-: condition एक तरह का filter होता है, जो वही items accept करता है जोकि TRUE valuate करे।
Example-:
newlist = [x for x in fruits if x != "apple"]
Output-:
['banana', 'cherry', 'kiwi', 'mango']
यह condition (if x != "apple") TRUE value return करेगा जब सारे elements new
List में होंगे except "apple" element के।
Iterable-: iterable का मतलब है की कोई भी non primitive data type like list, tuple, set etc.
Example-: iterable को हम range() function की सहायता से create कर सकते है।
mylist = [x for x in range(10)]
print(mylist)
Output-:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Expression-: expression iteration में current items होते है, लेकिन यह outcome भी है,
जिसे आप new list में list items की तरह end होने से पहले manipulate कर सकते हैं|
Example-:
fruits = ["apple", "banana", "cherry", "kiwi", "mango"]
mylist = [x.upper() for x in fruits]
print(mylist)
Output-:
['APPLE', 'BANANA', 'CHERRY', 'KIWI', 'MANGO']
List items में एक sort() method होती है जो list को bydefault alphanumerically ,ascending order में change करता है|
Example-:
numbers = [100, 50, 65, 82, 23]
numbers.sort()
print(numbers)
Output-:
[100, 82, 65, 50, 23]
Sort Descending-: list items को descending order में change करने के लिए हम sort() function में “reverse=true” argument pass करना होता है|
Example-:
frutiname = ["orange", "mango", "kiwi", "pineapple", "banana"]
fruitname.sort(reverse = True)
print(fruitname)
Output-:
['pineapple', 'orange', 'mango', 'kiwi', 'banana']
Case Insensitive Case-: sort method by default case sensitive होता है जिसमें uppercase letter, lowercase letters से पहले sort होते हैं|
Example-:
fruit = ["banana", "Orange", "Kiwi", "cherry"]
fruit.sort()
print(fruit)
Output-:
['Kiwi', 'Orange', 'banana', 'cherry']
Reverse Order-: इस function का use हम elements को reverse order में change करने के लिए करते हैं|
Example-:
games = ["cricket", "chess", "hockey", "football"]
games.reverse()
print(games)
Output-:
['football', 'hockey', 'chess', 'cricket']
आप केवल list2 = list1 टाइप करके list की copy नहीं बना सकते, क्योंकि: list2 केवल list1 का reference होगा, और list1 में किए गए changes automatic रूप से list2 में भी किए जाएंगे।
list में हम copy() function की सहायता से एक list को दूसरी list में copy कर सकते हैं|
Example-:
oldlist = ["manish", "naveen", "siddhika"]
newlist = thislist.copy()
print(newlist)
Output-:
["manish", "naveen", "siddhika"]
Python में हम दो या दो से अधिक lists को कई तरीकों से join कर सकते है-
1) + operator-: इस operator की सहायता से हम किन्हीं दो lists को join कर सकते है।
Example-:
list1 = ["a", "b", "c"]
list2 = [1, 2, 3]
list3 = list1 + list2
print(list3)
Output-:
['a', 'b', 'c', 1, 2, 3]
append()-: इस function के use से हम एक list को दूसरी list one by one करके join कर सकते है।
Example-:
list1 = ["a", "b" , "c"]
list2 = [1, 2, 3]
for x in list2:
list1.append(x)
print(list1)
Output-:
['a', 'b', 'c', 1, 2, 3]
extend()-: इस function के use से भी हम एक list को दूसरी list में join कर सकते है।
Example-:
list1 = ["a", "b" , "c"]
list2 = [1, 2, 3]
list1.extend(list2)
print(list1)
Output-:
['a', 'b', 'c', 1, 2, 3]
Python में list methods कई प्रकार के होते है -
append()-: इस function का use हम list में elements को add करने के लिए करते है।
Example-:
newfruit = ['apple', 'banana', 'cherry']
newfruit.append("orange")
print(newfruit)
Output-:
['apple', 'banana', 'cherry', 'orange']
clear()-: इस function के through हम list के सारे elements को remove कर सकते है।
Example-:
newfruit = ['apple', 'banana', 'cherry', 'orange']
newfruit.clear()
Output-:
[ ]
copy()-: यह function किसी specified list की copy को return करता है।
Example-:
newfruit = ['apple', 'banana', 'cherry', 'orange']
a = newfruit.copy()
print(a)
Output-:
['apple', 'banana', 'cherry']
count()-: यह function list में number of elements के साथ उसकी specified value को return करता है।
Example-:
newfruit = ["apple", "banana", "cherry"]
a = newfruit.count("cherry")
print(a)
Output-:
1
index()-: यह function element की index value को return करता है।
Example-:
newfruit = ['apple', 'banana', 'cherry']
a= newfruit.index("cherry")
print(a)
Output-:
2
Insert()-: इस function का use हम element को किसी specified position पर add करने के लिए use करते हैं।
Example-:
newlist = ['apple', 'banana', 'cherry']
newlist.insert(1, "orange")
print(newlist)
Output-:
['apple', 'orange', 'banana', 'cherry']
Python में list एक mutable data type होता है, जिसमें आप multiple elements को store करते है।
जिसे आप square bracket [] में लिखते है।
आप जानते होगे की जब भी हम list को create करते है तो उसे हम comma (,) से separate करते है।
List को create करते समय हम उसमे multiple data type(string, integer etc) का प्रयोग कर सकते है।
Example-:
newlist = [1,2,3,4,5]
print(newlist)
Output-:
[1,2,3,4,5]
इस तरह से हम किसी भी list को create कर सकते है।
List एक non primitive data type है जिसका प्रयोग हम multiple elements के collection को store करने के लिए करते है।
Example-:
mylist = ["Sumit","Sameer","Shivam","Sachin"]
print(mylist)
Output-:
["Sumit","Sameer","Shivam","Sachin"]
• Python में list ordered, changeable, duplicate value allow करते है।
• list elements को हम उसके index number के through access कर सकते है।
• list element mutable होते है।
• यह एक non primitive data type होता है।
• list को हम square bracket [] में लिखते है।
जब हम किसी list को clear() function की सहायता से empty कर देते है, तो वो list हमारी empty list कहलाती है।
Example-:
newlist = ["apple", "mango", "cherry"]
newlist.clear()
print(newlist)
Output-:
[]
I Hope आपको list क्या होता है? List python में कैसे work करता है, और list के functions के बारे में आपको जानकारी मिली होगी।यदि कोई शिकायत है तो comment section में comment कीजिए।