Python Forum

Full Version: convert multi list to one list !
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hello all ...
this is my code :
#qassam = raw_input()


def loop():
        
    for i in range(12):
        qan = "Key.f{}".format(i+1)
        print qan.split()
'''
    if qassam in qan:
        print "yes"

    else:
        print "no"
'''





loop()
output :

Output:
['Key.f1'] ['Key.f2'] ['Key.f3'] ['Key.f4'] ['Key.f5'] ['Key.f6'] ['Key.f7'] ['Key.f8'] ['Key.f9'] ['Key.f10'] ['Key.f11'] ['Key.f12']
i need to convert them to one list like [ 'Key.f1' , 'Key.f2' , 'Key.f3' ....ect ]
Hello, you can do something like this:
def loop():
    my_list = []    

    for i in range(12):
        qan = "Key.f{}".format(i+1)
        my_list.append(qan)
        print my_list
It's also a hint on upgrading to make this work for you Wink
>>> [f"Key.f{i}" for i in range(1, 13)]
['Key.f1',
 'Key.f2',
 'Key.f3',
 'Key.f4',
 'Key.f5',
 'Key.f6',
 'Key.f7',
 'Key.f8',
 'Key.f9',
 'Key.f10',
 'Key.f11',
 'Key.f12']
(Nov-18-2018, 08:46 AM)j.crater Wrote: [ -> ]Hello, you can do something like this:
def loop():
    my_list = []    

    for i in range(12):
        qan = "Key.f{}".format(i+1)
        list.append(qan)
        print my_list

this works :
def loop():

    my_list = []    
 
    for i in range(12):
        qan = "Key.f{}".format(i+1)
        my_list.append(qan)
    print my_list
Output:
['Key.f1', 'Key.f2', 'Key.f3', 'Key.f4', 'Key.f5', 'Key.f6', 'Key.f7', 'Key.f8', 'Key.f9', 'Key.f10', 'Key.f11', 'Key.f12']
thank u my friend <3

(Nov-18-2018, 09:49 AM)snippsat Wrote: [ -> ]It's also a hint on upgrading to make this work for you Wink
>>> [f"Key.f{i}" for i in range(1, 13)]

thank u for ur help <3