0

There are two types of list in Python, [[x], [y], [z]] and [x, y, z]. At least in the variable explorer both are stated as "list".

What is the difference? Can I work with them together? How can I transfer one into the other to actually work with them together?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Florida Man
  • 2,021
  • 3
  • 25
  • 43
  • 4
    ...what? Yes, they're both lists. One of them is a list that contains lists, the other isn't. Or maybe it is, depending on what `x`, `y` and `z` are. There *aren't* *"two types of list in Python"*, and it's not at all clear what gave you the idea that there were. – jonrsharpe Dec 21 '16 at 21:37
  • There is no "two types of list". There is "list" which is an object which can contains anything. So, a list can contain numbers, or others lists, which itself can contain other lists. – Delgan Dec 21 '16 at 21:38
  • 1
    ah, I see. Listception. – Florida Man Dec 21 '16 at 21:38
  • Thanks for your quick answer. I will work with that. – Florida Man Dec 21 '16 at 21:39

1 Answers1

4

[[x],[y],[z]] is list of lists whereas [x, y, z] is just a list.

In order to convert them, you may use list comprehensions as:

# convert list -> list of lists
>>> my_list = [1, 2, 3]
>>> [[i] for i in my_list]
[[1], [2], [3]]

# convert list of lists -> list
>>> my_list_of_lists = [[1], [2], [3]]
>>> [j for i in my_list_of_lists for j in i]
[1, 2, 3]
Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126