2

How would I go about making a list like

My_list = [['Item1', 'item2'], ['shark', 'dog', 'cat']]

To two lists like the following:

My_list = ['Item1', 'item2']
My_list2 = ['shark', 'dog', 'cat']

Also, how would I go about doing this if I didn't know how many lists were in the list?

4
  • 4
    What exactly is the meaning of My_list = ['Item1', 'item2']['shark', 'dog', 'cat']? It is not a valid expression. Commented Apr 10, 2013 at 6:54
  • If you wanted My_list = ['Item1', 'item2', 'shark', 'dog', 'cat'] then the answer is: sum(My_list,[]) or list(itertools.chain.from_iterable(My_list)). See also this related question. Commented Apr 10, 2013 at 6:56
  • Sorry, revised the question was a little confused art what I was asking lol. Commented Apr 10, 2013 at 6:58
  • please use lowercase variable names Commented Apr 10, 2013 at 7:48

5 Answers 5

1

You can use sequence unpacking:

My_list, My_list2 = My_list

If My_list can have more elements and you wanted to do something like:

for elem in My_list:
    create_variable('My_listn', elem)

This is not possible in the general case. Python doesn't allow creating locals at runtime(at least not reliably).

Sign up to request clarification or add additional context in comments.

2 Comments

Do I need to import the create_variable function? It says global name 'create_variable' is not defined
@VEDYL No, that's pseudocode. What I wanted to say is that you can't create variables at runtime. You can create global variables using globals()['variable_name'] = value, but this doesn't work for variables inside functions.
1

Assuming My_list is really meant to look like:

My_list = [['Item1', 'item2'], ['shark', 'dog', 'cat']]

And also that you truly want what you are asking for, you could do something like:

My_list2 = My_list[1]
My_list = My_List[0]

Or, alternatively, as others are suggesting, you could use list unpacking:

My_list, My_list2 = My_list

Comments

1
>>> My_list = [['Item1', 'item2'], ['shark', 'dog', 'cat']]
>>> l1, l2 = My_list
>>> l1
['Item1', 'item2']
>>> l2
['shark', 'dog', 'cat']

Comments

1
My_list, My_list2 = My_list[0], My_list[1]

2 Comments

Cheers, just curious, how would I do this if I didn't know how long the list was. So say the list was [['Item1', 'item2'], ['shark', 'dog', 'cat']['added', 'silly', 'cant', 'think']] but we didn't know there 3 lists inside of the list
you can't have unknown number of local variables - in that case you use list (which you already have)
0
>>> My_list = ['Item1', 'item2'],['shark', 'dog', 'cat']
>>> My_list
(['Item1', 'item2'], ['shark', 'dog', 'cat'])
>>> len(My_list)
2

>>> My_list[0]
['Item1', 'item2']
>>> My_list[1]
['shark', 'dog', 'cat']

>>> My_list[2]
Traceback (most recent call last):
  File "<pyshell#34>", line 1, in <module>
    My_list[2]
 IndexError: tuple index out of range

>>> My_list, My_list2 = My_list
>>> My_list
['Item1', 'item2']
>>> My_list2
['shark', 'dog', 'cat']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.