Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
+3 votes
3 views
in Python by (2.6k points)
How to remove an element from a list by index in Python, I tried list.remove method but it search the list and remove it but I don't want it to perform any search. How can I do this?

2 Answers

0 votes
by (46k points)
edited by

You can use del   , it also supports slices:

>>> q = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> del a[-1]
>>> a
[1, 2, 3, 4, 5, 6, 7, 8, 9]

Alternatively you can also use pop :

q = ['q', 'w', 'e', 'r']
q.pop()

# now q is ['q', 'w', 'e']

Learn more about Python from an expert. Enroll in our Python Certification Training Course.

0 votes
by (106k points)

You probably want pop:

a = ['a', 'b', 'c', 'd']

a.pop(1)

You can use the following video tutorials to clear all your doubts:-

Related questions

+1 vote
2 answers
+3 votes
2 answers
0 votes
2 answers
asked May 30, 2019 in Python by Anvi (10.2k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...