python - Looping Function to Define variables -


im trying create function takes list , assigns each of strings in list variable when dont know how many strings in list

here tried:

examplelist = ['turtle','cow','goat','pig','swag']  def add_one(list):     x = "a"+"1"     y = 0     y = y+1     x = list[y]   while true:     add_one(examplelist) 

so im taking example list im using a1 define examplelist[1] want loop , assign a11 examplelist[2] , on

for output im trying get:

a1 = examplelist[1] a11 = examplelist[2] a111 = examplelist[3] a1111 = examplelist[4] 

and on

i know isnt right way im trying show guys trying

if knows how correctly please help!

i think you're trying do. don't know why on earth you're trying it, can this:

example_list = ['turtle','cow','goat','pig','swag'] number_of_ones = 1 item in example_list:     globals()['a'+('1'*number_of_ones)] = item     number_of_ones += 1  print(a11111) # prints 'swag' 

if want little shorter, use enumerate:

example_list = ['turtle','cow','goat','pig','swag'] number_of_ones, item in enumerate(example_list, 1):     globals()['a'+('1'*i)] = item  print(a11111) # prints 'swag' 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -