Remove an Element from a List in Python

Remove an Element from a List in Python

remove(): The remove() method searches for the given element in the list and removes the first matching element. If the item present multiple times then only first occurrence will be removed.

remove() मेथड लिस्ट में दिए गए एलीमेंट को सर्च करती है और पहले मैचिंग एलीमेंट को हटाती है। यदि आइटम कई बार मौजूद होता है तो केवल पहली घटना को हटा दिया जाएगा।

remove() method only removes one element at a time, to remove range of elements, iterator is used.

remove() मेथड केवल एक एलीमेंट को एक बार में हटाता है, एलीमेंट की श्रेणी को हटाने के लिए, ईटरेटर का उपयोग किया जाता है।

remove() function raises an Error if element doesn’t exist in the set.

remove() फंक्शन एक एरर रेज करता है, यदि एलीमेंट सेट में मौजूद नहीं है।

Syntax: list.remove(element)

  • The remove() method takes a single element as an argument and removes it from the list.

remove () मेथड एक एलीमेंट को एक तर्क के रूप में लेती है और इसे लिस्ट से निकाल देती है।

  • If the element(argument) passed to the remove() method doesn’t exist, valueError exception is thrown.

यदि एलीमेंट (तर्क) को हटा दिया गया remove() मेथड मौजूद नहीं है, तो वैल्यू एरर अपवाद दिखेगा।

  • The remove() doesn’t return any value.

remove() किसी भी वैल्यू को वापस नहीं करता है।

Example 1:
Ist = [10, 20,30, 40]
print (Ist)
Ist.remove (30)
print (Ist)

Output:
[10, 20, 30, 40]
[10, 20, 40]

Example 2: If the specified element not present in list we will get an error message
यदि निर्दिष्ट एलीमेंट लिस्ट में मौजूद नहीं है, तो हमें एक त्रुटि संदेश मिलेगा
Ist = [10, 20,30,40]
print (Ist)
Ist.remove (30)
print (Ist)

Output:
[10, 20, 30, 40]
[10, 20, 40]

Example 3: If the item present multiple times then only first occurrence will be removed.
यदि आइटम कई बार मौजूद होता है तो केवल पहली घटना को हटा दिया जाएगा।
Ist = [10, 20,30, 40, 20]
print (Ist)
Ist.remove (20)
print (Ist)

Output:
[10, 20, 30, 40, 20]
[10, 30, 40, 20]

pop(): pop() function can also be used to remove and return an element from the set, but by default it removes only the last element of the set, to remove element from a specific position of the List, index of the element is passed as an argument to the pop() method.

pop() फंक्शन का उपयोग सेट से किसी एलीमेंट को निकालने और रिटर्न करने के लिए भी किया जा सकता है, लेकिन डिफॉल्ट रूप से यह लिस्ट के विशिष्ट स्थान से एलीमेंट को हटाने के लिए, सेट के केवल अंतिम एलीमेंट को हटाता है, इंडेक्स एलीमेंट को pop() मेथड के तर्क के रूप में पारित किया जाता है।

Syntax: list.pop(index)

  • The pop() method takes a single argument (index) and removes the item present at that index.

pop() मेथड एकल तर्क (इंडेक्स) लेती है और उस इंडेक्स पर मौजूद आइटम को हटा देती है।

  • If the index passed to the pop() method is not in range, it throws IndexError: pop index out of range exception.

यदि pop() मेथड में दिया गया इंडेक्स सीमा में नहीं है, तो यह इंडेक्स एरर को थ्रो कर देता है : पॉप इंडेक्स सीमा के बाहर।

  • The parameter passed to the pop() method is optional. If no parameter is passed, the default index -1 is passed as an argument which returns the last item.

pop() मेथड के लिए पारित पैरामीटर वैकल्पिक है। यदि कोई पैरामीटर पारित नहीं होता है, तो डिफॉल्ट इंडेक्स- 1 को एक तर्क के रूप में पारित किया जाता है, जो अंतिम आइटम रिटर्न करता है।

Example 1:
Ist = [10, 20, 30]
print (Ist)
Ist.pop()
print (Ist)
Ist.pop ()
print (Ist)
Ist.pop ()
print (Ist)

Output:
[10, 20, 30]
[10, 20]
[10]
[]

Example 2: If the list is empty then pop function raises an error message.
यदि लिस्ट खाली है, तो pop() फंक्शन एक एरर मैसेज रेज करता है। 
Ist = []
print (Ist)
Ist.pop ()

Output:
[]
Traceback (most recent call last):
  File “C:/Users/HP/Desktop/test.py”, line 3, in <module>
    Ist.pop ()
IndexError: pop from empty list

Difference between remove () and pop ()
remove () और pop() में अंतर

remove()pop()
We can use to remove special element from the list. हम लिस्ट से स्पेशल एलीमेंट को हटाने के लिए कर सकते हैंWe can use to remove last element from the list. हम अंतिम एलीमेंट को सूची से हटाने के लिए उपयोग उपयोग कर सकते हैं
It can’t return any value. यह किसी भी मूल्य को वापस नहीं कर सकता हैIt returned remove element. यह एलीमेंट को वापस लौटाता है
If special element not available, then we get value error. यदि विशेष एलीमेंट उपलब्ध नहीं है तो हमें वैल्यू एरर मिलती हैIf list is empty, then we get an error. यदि लिस्ट इम्पटी है तो हमें एक एरर मिलती है।

Note: List objects are dynamic i.e., based on our requirement we can increase and decrease the size.

लिस्ट ऑब्जेक्ट डॉयनॉमिक होते हैं यानी, हमारी आवश्यकता के आधार पर हम आकार को बढ़ा और घटा सकते
1. append(), insert(), extend(): for increasing the size
append(), insert(), extend(): आकर बढ़ाने के लिए  
2. remove(), pop(): for decreasing the size
remove(), pop(): आकर घटाने के लिए  

Ordering elements of List
लिस्ट के एलिमेंट का आदेश देना

There are other several ways to reverse a list. – किसी लिस्ट को उलटने के लिए कई अन्य तरीके

reverse (): We can use to reverse() order of elements of list. The reverse () function doesn’t take any argument. The reverse () function doesn’t return any value. It only reverses the elements and updates the list.

हम लिस्ट के एलिमेंट के reverse() आर्डर का उपयोग कर सकते हैं । reverse() फंक्शन कोई तर्क नहीं लेता है । reverse() फंक्शन किसी भी वैल्यू को रिटर्न नहीं करता है । यह केवल एलिमेंट को उलट देता है और लिस्ट को अपडेट करता है।

Syntax: list.reverse()
Example 1:
Ist = [10,20,30,40]
print (“Before reversing list items”)
print (Ist)
Ist.reverse()
print(“After reversing list items”)
print (Ist)

Output:
Before reversing list items
[10, 20, 30, 40]
After reversing list items
[40, 30, 20, 10]

Example 2:
Ist = [‘a’, ‘b’, ‘c’,’d’]
print (“Before reversing list items”)
print (Ist)
Ist.reverse()
print (“After reversing list items”)
print (Ist)

Output:
Before reversing list items
[‘a’, ‘b’, ‘c’, ‘d’]
After reversing list items
[‘d’, ‘c’, ‘b’, ‘a’]

Chapter wise Model Paper Link

About Me