सूचकांक () विधि सूची में निर्दिष्ट तत्व का सूचकांक लौटाती है।
सूची index()
विधि का सिंटैक्स है:
list.index (तत्व, प्रारंभ, अंत)
सूची सूचकांक () मापदंडों
सूची index()
विधि अधिकतम तीन तर्क ले सकती है:
- तत्व - खोजा जाने वाला तत्व
- प्रारंभ (वैकल्पिक) - इस सूचकांक से खोज शुरू करें
- अंत (वैकल्पिक) - इस सूचकांक तक तत्व को खोजें
सूची सूचकांक से वापसी मान ()
index()
विधि सूची में दिए गए तत्व के सूचकांक देता है।- यदि तत्व नहीं मिला है, तो एक
ValueError
अपवाद उठाया जाता है।
नोट:index()
विधि केवल मिलान तत्व की पहली आवृत्ति देता है।
उदाहरण 1: तत्व के सूचकांक का पता लगाएं
# vowels list vowels = ('a', 'e', 'i', 'o', 'i', 'u') # index of 'e' in vowels index = vowels.index('e') print('The index of e:', index) # element 'i' is searched # index of the first 'i' is returned index = vowels.index('i') print('The index of i:', index)
आउटपुट
E: 1 का सूचकांक i: 2 है
उदाहरण 2: सूची में मौजूद तत्व का सूचकांक नहीं
# vowels list vowels = ('a', 'e', 'i', 'o', 'u') # index of'p' is vowels index = vowels.index('p') print('The index of p:', index)
आउटपुट
ValueError: 'p' सूची में नहीं है
उदाहरण 3: इंडेक्स का काम () स्टार्ट एंड एंड पैरामीटर्स के साथ
# alphabets list alphabets = ('a', 'e', 'i', 'o', 'g', 'l', 'i', 'u') # index of 'i' in alphabets index = alphabets.index('e') # 2 print('The index of e:', index) # 'i' after the 4th index is searched index = alphabets.index('i', 4) # 6 print('The index of i:', index) # 'i' between 3rd and 5th index is searched index = alphabets.index('i', 3, 5) # Error! print('The index of i:', index)
आउटपुट
E: 1 का सूचकांक: 1 का सूचकांक: 6 ट्रैसबैक (सबसे हालिया कॉल अंतिम): फ़ाइल "* लैप; स्ट्रिंग>", पंक्ति 13, वैल्यूएयर में: 'i' सूची में नहीं है