0

I have the following tuple (list_permutation):

[(1,), (2,), (3,), (1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2)]

And I want to convert it in a list that looks like:

[[1],[2],[3],[1,2],[1,3]]...

This is my code that I've already tried:

result = [int(x) for x, in list_permutation]
print(result)

But I'm getting this error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-63-fc7f88d67875> in <module>
----> 1 result = [int(x) for x, in list_permutation]
      2 print(result)

<ipython-input-63-fc7f88d67875> in <listcomp>(.0)
----> 1 result = [int(x) for x, in list_permutation]
      2 print(result)

ValueError: too many values to unpack (expected 1)
Pika Supports Ukraine
  • 3,612
  • 10
  • 26
  • 42
Marek
  • 43
  • 1
  • 6

4 Answers4

4
l = [(1,), (2,), (3,), (1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2)]
new_l = [list(x) for x in l]
print(new_l)

This yields [[1], [2], [3], [1, 2], [1, 3], [2, 1], [2, 3], [3, 1], [3, 2]].

Filip Dimitrovski
  • 1,576
  • 11
  • 15
0

You can use the in built map function:

l = [(1,), (2,), (3,), (1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2)]
l = list(map(list, l))
print(l)

This should give you [[1], [2], [3], [1, 2], [1, 3], [2, 1], [2, 3], [3, 1], [3, 2]]

yeshks
  • 101
  • 6
0

Using map this can be achieved easily

perm = [(1,), (2,), (3,), (1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2)]
perm_as_list = map(list, perm)

Output below

In [1]: perm = [(1,), (2,), (3,), (1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2)]
In [2]: perm_as_list = map(list, perm)
In [3]: perm_as_list
Out[3]: [[1], [2], [3], [1, 2], [1, 3], [2, 1], [2, 3], [3, 1], [3, 2]]
kumar
  • 2,570
  • 2
  • 17
  • 18
0

list function can convert a tuple to list you have a list of tuples so do the folowing:

list_permutation=[(1,), (2,), (3,), (1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2)]
result = [list(x) for x in list_permutation]
neferjina
  • 31
  • 1
  • 6