Creating lists while code is running and adding them to an List Parent

Viewed 140

I am trying to create lists based on a value given by the user input and then I would like to add those lists created to a Parent list where they all can be accessed and used.

For example, I want the for loop to grab the number entered by the user, and based on that number it needs to create lists numbered from 1 to whatever they chose. At the end of each repetition I would like to add the created list to a parent list.

Here is what I have so far, I am struggling in adding them to the parent list I guess.

lists = int(input("How many lists do you want? "))
varname = 'iteration_number_'
parent_list = []

for i in range(lists):
    iteration_number = i+1 #So it does not start from 0
    iteration_names = varname + str(iteration_number) #merging the name and the number

    x = parent_list.append(exec(f"{iteration_names} = []")) #creating lists with that name

try:
    iteration_number_1.append("Cow") # appends Cow to the first list if existing
    iteration_number_2.append("Moo") # appends Moo to the first list if existing

    print(iteration_number_1)
    print(iteration_number_2)

except NameError:
    pass

print(parent_list)
parent_list[0].append("This is list iteration_number_1 but I'm not working")

The last part of the code doesn't work as planned. In my head when I print parent_list I should get [[iteration_number_1], [iteration_number_2]] and they can be accessed like this

parent_list[0].append("Hello") #appending to the iteration_number_1 list

Does anyone know a better idea? or how to make this idea work?

2 Answers

The programmers call them nested lists:

myNestedList = [
    [1, 2, 3], 
    [4, 5, 6], 
    [7, 8, 9]
]

You can access the inner lists this way:

>>> myNestedList[0] # Get the first row
[1, 2, 3]
>>> myNestedList[0][2]
3

You can append elements to the inner lists this way:

>>> myNestedList[0].append(10)
>>> myNestedList
[[1, 2, 3, 10], [4, 5, 6], [7, 8, 9]]

I saw you are using strings as keys instead of integers, this way:

iteration_names = varname + str(iteration_number) #merging the name and the number
x = parent_list.append(exec(f"{iteration_names} = []")) #creating lists with that name

In your case it's a bad idea, but you could use dictionaries, this way:

myDict: dict = {
    "iteration_name_1": <first-element-of-the-list>,
    "iteration_name_2": <second-el...>
}

You can access the dictionary this way:

>>> myDict['iteration_name_1']
<first-element...>

If you tried this at the end of the program:

parent_list[0].append(iteration_name_1)

and it didn't work, I would suggest to create the lists by name this way (if you really need to call them with names like 'name_x'):

>>> locals()['iteration_name_1'] = []
>>> iteration_name_1
[]

It has nothing to do with the question, but...

for i in range(lists):
iteration_number = i+1 #So it does not start from 0

...you should know that a better practice is to do this:

for i in range(1, lists):
    # i starts from 1

All and all...


The best practice would be doing this way:

for i in range(lists):
    parent_list.append([])
try:
    parent_list[0].append('Cow')
    parent_list[1].append('Moo')
except IndexError: # There is a different exception for an element not found in parent_list
    pass

It worked for me

You could try to a dictionary with the key as a string to the iteration number and the value as a list:

d = {}
d["iteration_number_1"] = []
...

d["iteration_number_1"].append("hello")
Related