0

Might be a stupid question but I am not even sure how to ask it in a search. Been searching for quite some time on how to do this, seems like something that's quite basic that I have somehow just glossed over at some point!

So, say I have a group of lists:

List_1 = [1,2,3]
List_2 = [4,5,6]
List_3 = [7,8,9]
List_4 = [10,11,10]

I want to be able to create something like:

SelectList = 0

for items in List_x:
         List_x.append(y)
         SelectList = SelectList + 1
         etc etc

And finally what is the terminology used for this kind of thing?

edit:

Looks like from the comments already what I am asking is possible but not in the way I thought it was in my head.

What I have is a some code that is very very very long just do the fact that it is arranged like:

for item in list_1:
    thing1 = dict1.get('item')
    test1[1].append(dict1.get('Bla'))


for item in list_2:
    thing1 = dict2.get('item')
    test2[1].append(dict2.get('Bla'))


for item in list_3:
    thing3 = dict3.get('item')
    test1[3].append(dict3.get('Bla'))

and was hoping there was a way to generate this like:


for item in list_x:
    thingx = dictx.get('item')
    thingx = dictx.get('item') 
    x = x + 1

  • I don't understand what you mean by "something like" the code in question. What should happen when you use the code? – Karl Knechtel Oct 18 '20 at 15:57
  • 1
    Are you trying to do [this](https://stackoverflow.com/questions/1373164/how-do-i-create-a-variable-number-of-variables)? That's sometimes referred to as "dynamic variables" or "variable variables". Just wrap those lists in another list instead though, then iterate the outer list. – Carcigenicate Oct 18 '20 at 15:57
  • Any time you are tempted to use variable with names like `List_1` and `List_2` and access them with `List_x` you should be using a list. That's what they are for. – Mark Oct 18 '20 at 16:01
  • Nearly always the correct solution is to use a dictionary instead. `List[0] = [1,2,3]` etc. – tripleee Oct 18 '20 at 16:04

3 Answers3

0

I don't know what terminolgy you're looking for, because all you seem to want to do is to iterate over lists. To do that, you'll need to put the lists in a list. Like so:

list_1 = [1, 2, 3]
list_2 = [4, 5, 6]
list_3 = [7, 8, 9]
list_4 = [10, 11, 10]

all_lists = [list_1, list_2, list_3, list_4]

for list_x in all_lists:
    for item in list_x:
         print(item)

This will print all items in all lists.

Or, if you want to it to closer resemble your code:

list_1 = [1, 2, 3]
list_2 = [4, 5, 6]
list_3 = [7, 8, 9]
list_4 = [10, 11, 10]

all_lists = [list_1, list_2, list_3, list_4]

selected_list = 0
while selected_list < len(all_lists):
    for item in all_lists[selected_list]:
         print(item)
         selected_list = selected_list + 1
Ted Klein Bergman
  • 9,146
  • 4
  • 29
  • 50
  • I think in my head I have been a bit too attached to the idea of writing List_x and having the x be the only thing that changes rather than having to list every list if you know what I mean. The way above means I have to write out each list before hand rather than just having x as some sort of string or var that can change dynamically. So imagine it's not just the first list, but lots List_x Parts_x Names_x – StrangeCalibur Oct 18 '20 at 16:16
0

You might want to use a new function that takes a list as a parameter. This way you dont have to identify the list by name.

def myfunc(mylist):
    for item in mylist:
        pass

As someone mentioned it before, you can store the lists in another list, or if you would like to name them, use a hash map / dictionary.

mydict = dict()
mydict['1'] = [1, 2, 3]
mydict['2'] = [4, 5, 6]
mydict['name'] = [7, 8, 9]

And you can loop through the keys, or get them by name.

for key in mydict:
    myfunc(mydict[key])

Or by name

selected = '1' # could be 2 or name or whatever
myfunc(mydict[selected])
nagyl
  • 1,644
  • 1
  • 7
  • 18
0

A program that changes itself may be tricky and hard to read, and should be done when there is no alternative or easiest way for doing the task, I think the best way to achieve what you want is using a dict:

lists = dict(List_1 = [1,2,3], List_2 = [4,5,6], List_3 = [7,8,9], List_4 = [10,11,10])

then you can iterate over it using:

for k, v in lists.items():
    # Do whatever you need. v is the list and k is the name of this list. 

If you need it sorted alphanumerically you can do something like this:

for k in sorted(lists.keys()):
   # Do whatever you need, you get the current list with lists[k]

Please let me know if it is enough clear.

  • Yeah I get what you mean. In this case I the same loop created for items that only change by number over and over a lot of times which is why I was looking for this shortcut. – StrangeCalibur Oct 18 '20 at 16:28
  • Yes, I just saw what you edited and my best suggestion is to refactor your code in a way in all those repeating variables are stored in data structures as dicts or lists. It will not only avoid you the need of whiting self modifying code, but also will improve the readability a lot. I know a refactor may seem painful and scary, but the technical debt is worse, even if it's an small project. – La Division De Ariza Oct 18 '20 at 17:45
  • 1
    Thank you for your feedback, I get what you mean and I will do that instead. Thank you for taking the time to help me out! – StrangeCalibur Oct 20 '20 at 08:29