Lazerhorse
Lazerhorse

Reputation: 123

Concatenating list names into strings python

I have lists such as:

list_1 = []
list_2 = []
list_3 = []

I want to put them all into a list where the list names are strings

list_final = ["list_1", "list_2", "list_3"]

Is there a simple way to do this?

Upvotes: 0

Views: 127

Answers (2)

Srinivas
Srinivas

Reputation: 656

The below shall work..

list = [list_1, list_2, list_3]
names=['list_1', 'list_2', 'list_3']
list_final = dict(zip(names,list))
print(list_final)
print(list_final['list_1'])
print(list_final['list_2'])
print(list_final['list_3'])

If you want to just put the names in the final list, you can do this:

list_final_names=[]
for name in list_final:
    list_final_names.append(name)
print(list_final_names)

Upvotes: -1

DanielB
DanielB

Reputation: 2958

Using a dict

my_lists = {
    "list_1": [],
    "list_2": [],
    "list_3": [],
}

The "names" ("keys" in terms of dict) are available as my_lists.keys() or by iterating my_lists:

for list_name in my_lists:
    print(list_name)

Or, for literally the sample given in the question:

list_final = list(my_lists)

Upvotes: 2

Related Questions