-2

If I have a python list like:

list = [(1,3), (2,4), (5,6), (7,8)] and I want to divide my list based on their index(0/1); how can I do that?

For example the splitted list should be:

l1 = [1,2,5,7] and l2 = [3,4,6,8]

Anyone know a simple technique for this? Thanks

  • You can use `zip`. and don't use builtin `list` as variable name. – Julien May 17 '22 at 06:34
  • Juste set the index as a variable in a list comprehension : `index = 1` and with this list comprehension : `result = [i[index] for i in initial_list]` (please dont use `list` as a variable name since it's a built-in function in Python) – Titouan L May 17 '22 at 06:36

0 Answers0