Python Forum
convert multi list to one list !
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
convert multi list to one list !
#1
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 ]
Reply
#2
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
Reply
#3
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']
Reply
#4
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  No matter what I do I get back "List indices must be integers or slices, not list" Radical 4 1,091 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  Delete strings from a list to create a new only number list Dvdscot 8 1,466 May-01-2023, 09:06 PM
Last Post: deanhystad
  List all possibilities of a nested-list by flattened lists sparkt 1 878 Feb-23-2023, 02:21 PM
Last Post: sparkt
  convert string to float in list jacklee26 6 1,820 Feb-13-2023, 01:14 AM
Last Post: jacklee26
  convert a list to links Pir8Radio 3 1,043 Nov-28-2022, 01:52 PM
Last Post: Pir8Radio
  Сheck if an element from a list is in another list that contains a namedtuple elnk 8 1,738 Oct-26-2022, 04:03 PM
Last Post: deanhystad
  convert this List Comprehensions to loop jacklee26 8 1,419 Oct-21-2022, 04:25 PM
Last Post: deanhystad
Question Keyword to build list from list of objects? pfdjhfuys 3 1,500 Aug-06-2022, 11:39 PM
Last Post: Pedroski55
  Convert list to interger Clives 5 1,543 May-09-2022, 12:53 PM
Last Post: deanhystad
  Split a number to list and list sum must be number sunny9495 5 2,201 Apr-28-2022, 09:32 AM
Last Post: Dexty

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020