0

For example:

list = [1,7,2,8,9,2,7,9,2,8,2] 

How can I remove two of the "2" without removing the others?

2
  • 1
    You could call .remove(2) twice. Commented Dec 5, 2022 at 21:57
  • Create a new list, and in a for loop, copy over just the elements you want. Commented Dec 5, 2022 at 21:57

2 Answers 2

0

You can find the index of the one(s) you want to delete and use the function pop(index) to delete it/them.

Exemple:

your_list.pop(5) # Delete the 5th element of your list 

Here's a little doc: https://www.programiz.com/python-programming/methods/list/pop

Sign up to request clarification or add additional context in comments.

Comments

0

This should work by removing an i element n times from a list arr.

arr = [1,7,2,8,9,2,7,9,2,8,2] def remove(arr, i, n = 1): for _ in range(n): arr.remove(i) remove(arr, 2, 3) print(arr) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.