-1

I have:

inventory1=['AA','EA','RW','Others']

I need a list that contains all elements except 'Others'. In differing lists, the position of 'Others' is not fixed. Sometimes it is last, and at other times, it is somewhere else. The list above can contain a very large number of strings.

I tried,

inventory2=inventory1
inventory2.remove('Others')

Does not work.

inventory2=inventory1.remove('Others')

doesn't even return a list.

This should be simple to do. Yet, this is not working.

user2751530
  • 197
  • 1
  • 2
  • 9
  • Please be more specific in your questions and tag correspondingly. The tag `list` engages a lot of people, where you could have filtered that by providing a bit more information, like programming language, question-related aspect, etc. – Giorgi Tsiklauri Jun 09 '20 at 21:08
  • Done as you asked. – user2751530 Jun 09 '20 at 21:23
  • Does this answer your question? [A quick way to return list without a specific element in Python](https://stackoverflow.com/questions/15738700/a-quick-way-to-return-list-without-a-specific-element-in-python) – mkrieger1 Jun 09 '20 at 21:37
  • `inventory2=inventory1` does not create a copy of the list as you might have thought. But `inventory2.remove('Others')` should have removed `'Others'` from it, not sure what you mean by "Does not work". – mkrieger1 Jun 09 '20 at 21:38

1 Answers1

0

Just tried the following in Python3.5 (running it in terminal):

inventory1=['AA','EA','RW','Others']
inventory1.remove('Others')
inventory1

The output is:

['AA', 'EA', 'RW']

So, I don't see where the problem is, since it seems to work as intended. 'Others' gets removed. A more cumbersome way would be:

  inventory1=['AA','EA','RW','Others']
  del inventory1[inventory1.index('Others')]
  inventory1

The output is the same. For more information, see this post.

Daniel B.
  • 659
  • 1
  • 9
  • 17