0

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?

7
  • You may wish to look at the dict type: docs.python.org/3/tutorial/datastructures.html#dictionaries Commented May 24, 2020 at 8:39
  • Can you please edit your question to clarify your expected output? [list_1, list_2, list_3] is not "a list where the list names are strings". Is your desired output ["list_1", "list_2", "list_3"]? Are you looking for a mapping from name to list, i.e. {"list_1": list_1, "list_2": list_2, "list_3": list_3}? Commented May 24, 2020 at 8:41
  • Does this answer your question? How do I concatenate two lists in Python? Commented May 24, 2020 at 8:41
  • The answer is no. Commented May 24, 2020 at 8:42
  • try ask with exampl Commented May 24, 2020 at 8:42

2 Answers 2

2

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)
Sign up to request clarification or add additional context in comments.

Comments

-1

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)

1 Comment

As per the question, a list of names (i.e. names) is the desired result, not the initial setup.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.