0

I want to remove multiples of 2 from a list. How do I do it?

a = list(range(1, 11))
print(a)
for ele in a:
    if ele % 2 == 0:
        a.pop(ele)
print(a)

Before the condition, a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

After the condition, I am expecting a = [1, 3, 5, 7, 9] But I get this error IndexError: pop index out of range

How do I get the indexes of the various multiples of 2 to pop them out?? I am a beginner.

1 Answers1

3

If you use pop on the list you are iterating on, you are modifying that list and you will run into issues like this, a better approach might be to create a new list and put your odd elements in that list.
In addition, you are using pop on using the value of the list instead of using it on the index which is incorrect. (https://docs.python.org/3/tutorial/datastructures.html#more-on-lists)

a = list(range(1, 11))
odd_list = []
for ele in a:
    if ele % 2 != 0:
        odd_list.append(ele)
print(odd_list)
#[1, 3, 5, 7, 9]
Devesh Kumar Singh
  • 20,259
  • 5
  • 21
  • 40
  • 1
    Please don't post answers on obviously off topic/bad questions! [See: **Should one advise on off topic questions?**](//meta.stackoverflow.com/q/276572) – Patrick Artner Apr 12 '19 at 15:57
  • 4
    Apologies for the ignorance, but I am not sure if it is off topic. The question was doing something fundamentally wrong, and I was just trying to give an example of what was wrong in the question, and tried to provide a correct solution. – Devesh Kumar Singh Apr 12 '19 at 16:04
  • 1
    In addition, folks are doing the same in the comment section above. I just provided an approach in form of an answer – Devesh Kumar Singh Apr 12 '19 at 16:06
  • 1
    As does the dupe for modifying a list you iterate over. Beside that its only 50% of the error - he uses `pop` on a value, not on the index of the value to be removed. Wich also has got dupes... – Patrick Artner Apr 12 '19 at 16:06
  • 1
    Did you read the link ? What you should do: **cast a down vote, vote to close** (flag to close if you're under 3K rep), **_comment and explain why their question is bad_ and _how to fix it_**, use the tour, the help, maybe even a chat session Don't ever answer an off-topic question, not via chat, comments, or any other way. It is even dangerous to say: go here, Google for that, because next time, they will do the exact same and eventually get what they want. – Patrick Artner Apr 12 '19 at 16:10
  • I did read the link, and I don' think the question is off topic/bad. The user has provided an example and showed what error he got by using it, in addition mentioning he is a beginner. I have seen worse questions then this answered on SO. Also if the question is bad, I am sure it will be closed, and then I will delete my answer. – Devesh Kumar Singh Apr 12 '19 at 16:16
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/191746/discussion-between-devesh-kumar-singh-and-patrick-artner). – Devesh Kumar Singh Apr 12 '19 at 16:19
  • @PatrickArtner I am actually new with Stackoverflow and python programming in general, so pardon. – Nii Laryea Quartey-Papafio Apr 15 '19 at 17:29
  • Please accept and upvote the answer if it helped you :) – Devesh Kumar Singh Apr 20 '19 at 12:32