1

I recently created a program that will give me a list of lists:

myList = [[1,2],[3,4],[5,6]]

However, I do not know how many lists this list will contain, but I do want to unpack it into unique variables that I can manipulate later (not a dictionary):

list1 = [1,2]
list2 = [3,4]
list3 = [5,6]

I have been trying to figure this out for a long time, but I can't. I would really appreciate some help.

aaron
  • 39,695
  • 6
  • 46
  • 102
John Bucel
  • 181
  • 1
  • 5
  • The real question is why would you want to do that? What's wrong with using directly `myList[1]`? – Julien Dec 01 '17 at 02:47
  • 3
    @Julien &John You mean "what is the X problem" in this question, as it is a typical example of the [XY Problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). – robinCTS Dec 01 '17 at 02:54
  • 1
    This is really not a good idea. Sure, you can use `globals()`, but then you need to keep using `globals()` to access your new variables. So you might as well just create your own `list` or `dict` to hold your objects. Using a `list` lets you access them by an index number. A `dict` gives you more options: you can give each object a name if you want. – PM 2Ring Dec 01 '17 at 03:14

3 Answers3

5

You actually don't need to unpack them at all: The individual lists can be referenced as unique variables as they are now!

Instead of trying to reference the variables as list1 with new names, use their existing names:

myList = [[1,2],[3,4],[5,6]]
print (myList[0]) # Prints out the list [1,2]
print (myList[1]) #Prints out the list [3,4]
print (myList[2]) #Prints out the list [5,6]

There are advantages to this format, take for example this made up list:

myListOfUnexpectedSize = [[1,3],[5,7],...more lists...,[15,17]]
print(len(myListOfUnexpectedSize)) #Prints out the number of lists you have
for lis in myListOfUnexpectedSize: #This loop will print out all the lists one by one
    print(lis) 
print(myListOfUnexpectedSize[-1]) #Prints out the last list in the big list

So by using the size of the larger list, you can figure out how many lists you have inside and work with them.

Davy M
  • 1,697
  • 4
  • 20
  • 27
  • Ajax's answer actually answers the question, whereas I just sidestep the question and show a different solution to the issue... people who are upvoting me should upvote that person as well. – Davy M Dec 01 '17 at 02:59
  • 5
    Sure, Ajax's answer does what the OP asked for, but really the OP shouldn't be doing that. They should be using a list, like your answer shows, or perhaps a dict. Manhandling `globals()` is rarely a good idea. And once you've created "variable variables" via `globals()` you have to keep using it to access those variables. – PM 2Ring Dec 01 '17 at 03:08
2

What you may want is a collection, such as lists to hold each list, and then you can reference it by index. Alternatively, you could consider a dictionary and a bit of list comprehension as follows if you really want to reference it by a specific name

listDict = {"list" + str(key+1) : value for (key, value) in enumerate(myList)}
Zooby
  • 325
  • 1
  • 7
0

If you may have more than three sublists, you can try this:

myList = [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]
list1, list2, list3, *rest = myList
print(list1)
print(list2)
print(list3)

the rest will hold the remaining sublists, and the list1, list2, list3 are the ones you want.

Menglong Li
  • 2,177
  • 14
  • 19