0

I have a list that contists of several elements:

list = ["a", "b", "c", "d", "e", "f", "g"]

I need a for-loop, that generates multiple lists, that contain all of the elements exept for one element and also want to name them appropriately. It should look something like this:


for i in list:
    globals()['list_' + str(i)] = list.remove(i)

The result should look like this:

list_a = ["b", "c", "d", "e", "f", "g"]
list_b = ["a", "c", "d", "e", "f", "g"]
list_c = ["a", "b", "d", "e", "f", "g"] 
list_d = ["a", "b", "c", "e", "f", "g"]
ect...

1 Answers1

1

You can use a dictionary:

lst = ["a", "b", "c", "d", "e", "f", "g"]
dic = {}
for item in lst:
    dic['list_' + item] = [x for x in lst if not x == item]
print(dic)

Output:

{'list_a': ['b', 'c', 'd', 'e', 'f', 'g'], 'list_b': ['a', 'c', 'd', 'e', 'f', 'g'], 'list_c': ['a', 'b', 'd', 'e', 'f', 'g'], 'list_d': ['a', 'b', 'c', 'e', 'f', 'g'], 'list_e': ['a', 'b', 'c', 'd', 'f', 'g'], 'list_f': ['a', 'b', 'c', 'd', 'e', 'g'], 'list_g': ['a', 'b', 'c', 'd', 'e', 'f']}
Erik McKelvey
  • 1,650
  • 1
  • 12
  • 23