How to access Elements of a List in Python

How to access Elements of a List in Python

In order to access the list items, refer to the index number. Use the index operator [ ] to access an item in a list. The index must be an integer. Nested list is accessed using nested indexing.

सूची आइटम तक पहुंचने के लिए इंडेक्स नंबर को देखें। लिस्ट में किसी आइटम तक पहुंचने के लिए इंडेक्स ऑपरेटर [] का उपयोग करें। इंडेक्स एक इंटीजर होना चाहिए। नेस्टेड इंडेक्सिंग का उपयोग करके नेस्टेड लिस्ट प्राप्त की जाती है।

Python supports both positive and negative index.

पायथन पॉजिटीव और निगेटिव दोनों तरह के इंडेक्स का समर्थन करता है।

Python supports both positive and negative indexes. Positive index means from left to right (0 to n) whereas Negative index means right to left (-1 to -n)

पायथन पॉजिटीव और निगेटिव दोनों तरह के इंडेक्स का समर्थन करता है। पॉजिटीव इंडेक्स का अर्थ बाएं से दाएं (0 to n) है जहां निगेटिव इंडेक्स का अर्थ है दाएं से बाएं (-1 to -n) है।

Positive index: Positive index means starts from left to right (forward direction). The first index has 0; the next index has 1, and so on. The last index will be the length of the list minus one.

पॉजिटीव इंडेक्सः पॉजिटीव इंडेक्स का मतलब बाएं से दाएं (आगे की दिशा) से शुरू होता है। पहले इंडेक्स में 0 है, अगले इंडेक्स में 1 है, और इसी तरह, अंतिम इंडेक्स सूची की लंबाई माइनस एक होगी।

Negative index: Negative index means starts from right to left (backward direction).The first index has -1; the next index has -2, and so on. The last index will be the length of the list.

निगेटिव इंडेक्सः निगेटिव इंडेक्स का मतलब दाएं से बाएं (पीछे की ओर) शुरू होता है। पहले इंडेक्स में -1 है, अगले इंडेक्स में -2 है, और इसी तरह, अंतिम इंडेक्स में लिस्ट की लंबाई होगी।

Example 1: Accessing of element from list using positive index

पॉजिटीव इंडेक्स का उपयोग करके लिस्ट से एलीमेंट की पहुंच
Ist = [“apple”, “banana”, “mango”]
print (“Printing list :”)
print (Ist)
print (“Accessing a element from the list”)
print (“Ist[0] : “, Ist[0])
print(“Ist[1] : “, Ist[1])
print (“Ist[2] : “, Ist[2])

Output:
Printing list :
[‘apple’, ‘banana’, ‘mango’]
Accessing a element from the list
Ist[0] :  apple
Ist[1] :  banana
Ist[2] :  mango

Example 2: Accessing of element from list using negative index
निगेटिव इंडेक्स का उपयोग करके लिस्ट से एलीमेंट की पहुंच

Ist = [“apple”, “banana”, “mango”]
print (Ist)
print (“Accessing a element from the list using -Ve index value”)
print(“Ist[-1] : “, Ist[-1])
print(“Ist[-2] : “, Ist[-2])
print (“Ist[-3] : “, Ist[-3])

Output:
[‘apple’, ‘banana’, ‘mango’]
Accessing a element from the list using -Ve index value
Ist[-1] :  mango
Ist[-2] :  banana
Ist[-3] :  apple

Example 3: Accessing of element from list using looping.
लूपिंग का उपयोग करके लिस्ट से एलीमेंट की पहुंच।

Ist = ["apple", "banana", "mango"] 
print("Printing list :") 
print (Ist) 
print ("Accessing a element from the list") 
for i in range (0,len (Ist)):
    print (i, " : ", Ist[i])

Output:
Printing list :
[‘apple’, ‘banana’, ‘mango’]
Accessing a element from the list
0  :  apple
1  :  banana
2  :  mango

Example 4: Accessing of element from a Multi-Dimensional list.
मल्टी-डायमेंशनल लिस्ट से एलीमेंट की पहुंच
Ist = [[1, 2] , [ ‘mango’, ‘banana’]]
print (“Accessing a element from a Multi-Dimensional list”)
print (‘Ist [0] [0]: ‘, Ist [0] [0])
print (‘Ist [0] [1] : ‘, Ist [0] [1])
print (‘Ist[ 1] [0] : ‘, Ist [1] [0])
print (‘Ist [1] [1] : ‘, Ist [1] [1])

Output:
Accessing a element from a Multi-Dimensional list
Ist [0] [0]:  1
Ist [0] [1] :  2
Ist[ 1] [0] :  mango
Ist [1] [1] :  banana

Adding Elements to a List
लिस्ट में एलीमेंट जोड़ना

append(): In Python, elements can be added to the List by using built-in function append(). Only one element at a time can be added to the list by using append() method, for addition of multiple elements with the append() method, loops are used. append() method added of elements at the end of the list.

पायथन में, एलीमेंट बिल्ट-इन फंक्शन append) का उपयोग करके लिस्ट में जोड़ा जा सकता है। एक बार केवल एक एलीमेंट को append() मेथड का उपयोग करके लिस्ट में जोड़ा जा सकता है, append() मेथड के साथ कई एलीमेंट के अलावा, लूप का उपयोग किया जाता है। append() मेथड लिस्ट के अंत में एलीमेंट को जोड़ता है।

insert(): append() method only works for addition of elements at the end of the List, for addition of element at the desired position, insert() method is used. Unlike append() which takes only one argument, insert() method requires two arguments(position, value).

append() मेथड केवल लिस्ट के अंत में एलीमेंट को जोड़ने के लिए काम करती है, वांछित स्थिति में एलीमेंट को जोड़ने के लिए, insert() मेथड का उपयोग किया जाता है। append) के विपरीत जो केवल एक तर्क लेता है, insert() मेथड के लिए दो तर्क (पोजीशन, वैल्यू) की आवश्यकता होती है।

extend(): This method is used to add multiple elements at the same time at the end of the list.

इस पद्धति का उपयोग लिस्ट के अंत में एक ही समय में कई एलीमेंट को जोड़ने के लिए किया जाता है।

Note: append() and extend() methods can only add elements at the end.
नोट: append) और extend() मेथड केवल अंत में एलीमेंट जोड़ सकती हैं।

Example 1: Addition of elements in a List – लिस्ट में एलिमेंट का जोड़
Ist = []
print (“Blank List : “)
print (Ist)
Ist.append (10)
Ist.append (30)
Ist.append (50)
print (“After Addition of Three elements: “)
print (Ist)

Output:
Blank List :
[]
After Addition of Three elements:
[10, 30, 50]
Example 2: Adding elements to the List using loop –
लूप का उपयोग करके लिस्ट में एलीमेंट को जोड़ना

Ist = [] 
for i in range (10, 20):
    Ist.append (i) 
print ("After Addition of elements from 10-19: ")
print (Ist)

Output:
After Addition of elements from 10-19:
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
Example 3: Adding elements to the List to a List –  लिस्ट में एलीमेंट को एलीमेंट में जोड़ना

a = [] 
for i in range (1, 4):
    a.append (i) 
print("\nList after Addition of elements from 1-3: ") 
print (a)
b = ['apple', 'boy'] 
b.append(a) 
print ("List after Addition of a List: ") 
print (b)

Output:
List after Addition of elements from 1-3:
[1, 2, 3]
List after Addition of a List:
[‘apple’, ‘boy’, [1, 2, 3]]

Example 4: Using Insert, Addition of Element at specific Position
स्पेसिफिक पोजीशन, इंसर्ट, एलीमेंट के जोड़ का उपयोग करना
Ist = [100, 200, 300]
Ist.insert (1, 400)
print (Ist)
Ist.insert(2, “your name”)
print (Ist)

Output:
[100, 400, 200, 300]
[100, 400, ‘your name’, 200, 300]

Example 5: Addition of multiple elements using Extend Method
एक्सटेंड मेथड का उपयोग करके कई एलीमेंट का जोड़
Ist = [1,2,3,4,5]
Ist.extend ([6, 7, 8])
print (“List after performing Extend Operation: “)
print (Ist)

Output:
List after performing Extend Operation:
[1, 2, 3, 4, 5, 6, 7, 8]

Chapter wise Model Paper Link

About Me