0

Suppose I have 3 lists,

list_of_fruits = ["apple", "banana", "orange"]

list_of_orange_things = ["carrot", "orange", "basketball"]

list_of_colors = ["orange", "green", "blue"]

Is there a way to delete "orange" from all lists simultaneously, without specifically addressing any individual list?

At a certain point while running the program, it will be important for me to remove "orange" from ALL lists. It would be helpful to have a method to remove "orange" from all lists at once, rather than removing it from each list individually.


Removing from each list individually allows for the possibility of error any time I change the number of lists in the program. It would require me to locate each place in the program where "orange" needs to removed from all lists, and double check that the lists that it will be removed from correspond to ALL lists in the program. It also creates unnecessary clutter. An alternative method would be preferred to reduce clutter and make error impossible.


My actual program procedurally generates content, and so new objects will be created and deleted all the time. It is important that the program is able to handle changes as much as possible without human interaction.

Colin Hancey
  • 219
  • 2
  • 8
  • 2
    Add your lists to some "registry" as you create them, instead of just binding them to a name. A dict is a good candidate.. You're dangerously close to asking about [variable variables](https://stackoverflow.com/questions/1373164/how-do-i-create-a-variable-number-of-variables). There're also some [XY](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) vibes. – Ilja Everilä Jun 17 '17 at 19:43
  • This is definitely a problem of variable variables, because the items that go into these lists will all be randomly generated (and will later be classes will more attributes). The number of lists doesn't change during the program execution, but I will be changing it as I further develop the program. – Colin Hancey Jun 28 '17 at 23:09

3 Answers3

2

You can try this:

s = "orange"

list_of_fruits = ["apple", "banana", "orange"]

list_of_orange_things = ["carrot", "orange", "basketball"]

list_of_colors = ["orange", "green", "blue"]

new_list = [list_of_fruits, list_of_orange_things,list_of_colors]

final_list = [[b for b in i if b != s] for i in new_list]

Output:

[['apple', 'banana'], ['carrot', 'basketball'], ['green', 'blue']]

This basic algorithm iterates over the lists contained in new_list and filters out any occurrence of the string s. But as @Willem Van Onsem pointed out, you will want to find a new way of storing your lists so you can iterate through everyone once, such as a list within a list.

Ajax1234
  • 69,937
  • 8
  • 61
  • 102
  • I am glad I could help. – Ajax1234 Jun 17 '17 at 19:52
  • I realized that this solution actually does not work for me because it leaves the individual lists unchanged. I modified your code a bit to do just that, which I provided below. You're pretty much spot on, though. Thanks! – Colin Hancey Jun 19 '17 at 06:34
0

Simply keep a list of all your lists, add a new list to that list when it is created, and when you need to delete something from all the lists, iterate through your list of lists.

John Dengis
  • 192
  • 1
  • 8
0

Okay, I borrowed from code provided by @Ajax1234 and modified it to work for my application.

His solution is almost perfect, except it left the individual lists unchanged. I added code to iterate through each list in the containing list and remove "orange" from each.

@John Dengis had it right from the start, but I wanted to provide the code.

Here is the code.

list_of_fruits = ["apple", "banana", "orange"]

list_of_orange_things = ["carrot", "orange", "basketball"]

list_of_colors = ["orange", "green", "blue"]

new_list = [list_of_fruits, list_of_orange_things, list_of_colors]


for lists in new_list:
    for item in lists:
        if item == "orange":
            lists.remove(item)

No errors, and it adapts dynamically as I add or remove lists to the program.

Like @John Dengis points out, this is a problem of iterating through lists in a list. There is a question that develops a very similar response here: Python - Iterating through list of list

Colin Hancey
  • 219
  • 2
  • 8