How do I retrieve a specific list using variable names in Python?
setattr(mod, 'dlist_{}'.format(i+1), list(map(int,input().split())))
With this code, you want to create a list from dlist_1 to dlist_n and then add a list within the list with a population.
for i in range(0,n):
templist+=1
What code should be included in the wlist.append(""?""")
For example, you want to append the first value of each list from the list from dlist_1 to dlist_n among the saved lists and add [1,3,5,7,9] to the wlist in this way.
python
dlists = { k:[] for k in input().split() }
In this way, dlists[1]
, dlists[2]
, ... An empty list of the equals is created. dlist_1
, .. Don't make the variable name dynamic like this.The dictionary in the list is sufficient.
for k, v in dlists.items():
wlist.append(v[0])
The items()
method is recommended when using a dictionary in a for statement. For questions, values()
can be used, but it is often used because keys and values are often used together.
© 2024 OneMinuteCode. All rights reserved.