लिस्ट एक कलेक्शन है जो ऑर्डर्ड और चेंजेबल होता है। Python में लिस्ट को स्क्वायर ब्रैकेट्स के साथ लिखा जाता है। आप इंडेक्स नंबर को रेफर करके लिस्ट आइटम्स को एक्सेस करते हैं। नेगेटिव इंडेक्सिंग का मतलब अंत से शुरू होता है, -1 लास्ट आइटम को रेफर करता है। आप रेंज की शुरुआत और अंत को स्पेसिफाई करके इंडेक्स की एक रेंज स्पेसिफाई कर सकते हैं। रेंज स्पेसिफाई करते समय, रिटर्न वैल्यू स्पेसिफाइड आइटम्स के साथ एक नई लिस्ट होगी।
उदाहरण
# वर्ड्स की लिस्ट से ट्रिपलेट्स
# वर्ड्स की लिस्ट इनिशियलाइज़ करना
list_of_words = ['I', 'am', 'Vishesh', 'and', 'I', 'like', 'Python', 'programming']
# लिस्ट कॉम्प्रिहेंशन का उपयोग करके
List = [list_of_words[i:i + 3]
for i in range(len(list_of_words) - 2)]
# लिस्ट प्रिंट करना
print(List)
# वर्ड्स की लिस्ट इनिशियलाइज़ करना
list_of_words = ['I', 'am', 'Vishesh', 'and', 'I', 'like', 'Python', 'programming']
# आउटपुट लिस्ट इनिशियलाइज़ करना
out = []
# लिस्ट की लंबाई ढूंढना
length = len(list_of_words)
# इटरेशन का उपयोग करके
for z in range(0, length - 2):
# 3 वर्ड्स जोड़ने के लिए एक टेम्प लिस्ट बनाना
temp = []
temp.append(list_of_words[z])
temp.append(list_of_words[z + 1])
temp.append(list_of_words[z + 2])
out.append(temp)
# आउटपुट प्रिंट करना
print(out)
आउटपुट
[['I', 'am', 'Vishesh'], ['am', 'Vishesh', 'and'], ['Vishesh', 'and', 'I'], ['and', 'I', 'like'], ['I', 'like', 'Python'], ['like', 'Python', 'programming']] [['I', 'am', 'Vishesh'], ['am', 'Vishesh', 'and'], ['Vishesh', 'and', 'I'], ['and', 'I', 'like'], ['I', 'like', 'Python'], ['like', 'Python', 'programming']]