Common Methods of List

Common Methods of List – हालांकि Slicing व Indexing के तरीकों को हम बहुत ही Flexible तरीके से Use करते हुए किसी भी तरह के List Operations Perform कर सकते हैं, लेकिन फिर भी कुछ Common Tasks को Accomplish करने के लिए Slicing Methods को Use करने के स्‍थान पर insert(), append(), pop(), remove(), sort(), reverse() जैसे कुछ Specific Methods को Use करना तुलनात्‍मक रूप से ज्‍यादा बेहतर व आसान होता है, जिनमें से कुछ को हमने Core Types Chapter के अन्‍तर्गत भी देखा था।

[code]
FileName: ListMethodsAndFunctions.py
lstName = ["KRISHNA","MURARI",]
print("List of Names: \n", lstName, "\n")

# Appending New Data Items in the List
print('After Appending "Bihari","Shyam","MOHAM","Madan" in the List')
lstName.append("Bihari")
lstName.append("Madan")
lstName = lstName + ["Shyam"]
lstName += ["MOHAN"]
print(lstName, "\n")

# Extending the List with Multiple New Data Items
lstName.extend(["MADAN","BANWARI"])
print("Now Names in the List are: \n", lstName, "\n")
lstName.sort()
print("After Default Sorting: \n", lstName, "\n")
lstName.sort(key=str.upper)
print("After Key Based Sorting: \n", lstName, "\n")

# Reversing the List
lstName.sort(key=str.upper, reverse=True)
print("After Key Based Reversed Sorting: \n", lstName, "\n")
lstName.reverse()
print("Reversed the List In-Place: \n", lstName, "\n")
lstNamesReversed = list(reversed(lstName))
print("Reversed the List In-Place: \n", lstNamesReversed, "\n")

# Deleting from the List
lstName.pop()
print("After Pop Operation on the List: \n", lstName, "\n")
lstName.remove("MOHAN")
print("After Removing 'MOHAN' from the List: \n", lstName, "\n")
del lstName[2]
print("After Deleting from Index Number 2: \n", lstName, "\n")
print("Total Data Items in the List:", len(lstName), "\n")

# Copying the List
cpyListNames = lstName.copy()
print("Copy of the List lstNames: \n", cpyListNames, "\n")
cpyListNames1 = list(lstName)
print("Similar Copy using list(lstName): \n", cpyListNames1, "\n")
cpyListNames2 = lstName[:]
print("Similar Copy using lstName[:] : \n", cpyListNames2, "\n")

Output
List of Names:
 ['KRISHNA', 'MURARI']
After Appending "Bihari", "Shyam", "MOHAM", "Madan" in the List
 ['KRISHNA', 'MURARI', 'Bihari', 'Madan', 'Shyam', 'MOHAN']
Now Names in the List are:
 ['KRISHNA', 'MURARI', 'Bihari', 'Madan', 'Shyam', 'MOHAN', 'MADAN', 'BANWARI']
After Default Sorting:
 ['BANWARI', 'Bihari', 'KRISHNA', 'MADAN', 'MOHAN', 'MURARI', 'Madan', 'Shyam']
After Key Based Sorting:
 ['BANWARI', 'Bihari', 'KRISHNA', 'MADAN', 'Madan', 'MOHAN', 'MURARI', 'Shyam']
After Key Based Reversed Sorting:
 ['Shyam', 'MURARI', 'MOHAN', 'MADAN', 'Madan', 'KRISHNA', 'Bihari', 'BANWARI']
Reversed the List In-Place:
 ['BANWARI', 'Bihari', 'KRISHNA', 'Madan', 'MADAN', 'MOHAN', 'MURARI', 'Shyam']
Reversed the List In-Place:
 ['Shyam', 'MURARI', 'MOHAN', 'MADAN', 'Madan', 'KRISHNA', 'Bihari', 'BANWARI']
After Pop Operation on the List:
 ['BANWARI', 'Bihari', 'KRISHNA', 'Madan', 'MADAN', 'MOHAN', 'MURARI']
After Removing 'MOHAN' from the List:
 ['BANWARI', 'Bihari', 'KRISHNA', 'Madan', 'MADAN', 'MURARI']
After Deleting from Index Number 2:
 ['BANWARI', 'Bihari', 'Madan', 'MADAN', 'MURARI']
Total Data Items in the List: 5
Copy of the List lstNames:
 ['BANWARI', 'Bihari', 'Madan', 'MADAN', 'MURARI']
Similar Copy using list(lstName):
 ['BANWARI', 'Bihari', 'Madan', 'MADAN', 'MURARI']
Similar Copy using lstName[:] :
 ['BANWARI', 'Bihari', 'Madan', 'MADAN', 'MURARI']
[/code]

इस Example में हमने लगभग सभी Common रूप से Use होने वाले Methods को Use कर लिया है और समान प्रकार से काम करने वाले सभी Expressions को एक Group के रूप में Specify किया है, ताकि आप समझ सकें कि किसी एक ही काम को आप कितने तरीकों से कर सकते हैं।

Python in Hindi - BccFalna.comये Article इस वेबसाईट पर Selling हेतु उपलब्‍ध EBook Python in Hindi से लिया गया है। इसलिए यदि ये Article आपके लिए उपयोगी है, तो निश्चित रूप से ये EBook भी आपके लिए काफी उपयोगी साबित होगी।

Python in Hindi | Page: 602 | Format: PDF

BUY NOW GET DEMO REVIEWS