-2

I want to separate different numbers in this list:

[([2437], [0.235]), ([4942], [0.217])]

and put them in new lists. Python returns length of this list equal to 2. I need to have two lists like these: [2437, 4942] and [0.235, 0.217].

How can I able to reach these lists from the above list in python?

Selcuk
  • 57,004
  • 12
  • 102
  • 110
Marziyeh Askari
  • 175
  • 1
  • 8
  • What have you tried to accomplish that and what technical issue have faced? – Tibebes. M Jun 16 '20 at 05:59
  • Your example looks like a matrix transpose: https://stackoverflow.com/questions/4937491/matrix-transpose-in-python. But your example is too simple. Will there always be 2 elements in a tuple? Are all tuple elements just a list of a single element? – awesoon Jun 16 '20 at 05:59
  • Does this answer your question? [Matrix Transpose in Python](https://stackoverflow.com/questions/4937491/matrix-transpose-in-python) – Osadhi Virochana Jun 16 '20 at 06:00

2 Answers2

1

If it's always two tuples of two single item lists, another way to unpack:

l1, l2 = [[inner_list[i][0] for inner_list in my_list] for i in range(2)]
kabanus
  • 24,623
  • 6
  • 41
  • 74
1

You can use the following method for Python 3:

>>> my_list = [([2437], [0.235]), ([4942], [0.217])]
>>> sub_list1, sub_list2 = [[*item1, *item2] for item1, item2 in zip(*my_list)]
>>> sub_list1
[2437, 4942]
>>> sub_list2
[0.235, 0.217]
Selcuk
  • 57,004
  • 12
  • 102
  • 110