Skip to content Skip to sidebar Skip to footer

How To Split Up A List Of Lists Python?

I have a list of lists myList = [[1,2,3],[4,5,6],[7,8,9,10]] and I want to split it up into three separate list, each with their own name: a = [1,2,3] b = [4,5,6] c = [7,8,9,10]

Solution 1:

You could unpack it directly:

a, b,c= myList

Solution 2:

python is easy, you can do

a,b,c=mylist

Solution 3:

To create new variables, you can use globals():

import string
myList = [[1,2,3],[4,5,6],[7,8,9,10]]for i, value in enumerate(myList):
   globals()[string.ascii_lowercase[i]] = value

print(a, b, c)

Output:

([1, 2, 3], [4, 5, 6], [7, 8, 9, 10])

Post a Comment for "How To Split Up A List Of Lists Python?"