-2

I have an list such as:

list = [[1, 3, 'orange'], [3, 5, 'apple'], [2, 3, 'orange'], [7, 9, 'pear']]

and i would like to convert it into multiple lists such as:

list1 = [[1, 3, 'orange'], [2, 3, 'orange']]
list2 = [3, 5, 'apple']
list3 = [7, 9, 'pear']

Thank you.

suso
  • 11
  • 2
  • 1
    Have you tried yet? Can you show us the code to help you? [Why is “Can someone help me?” not an actual question?](https://meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question) – Holden May 02 '20 at 22:22
  • 1
    Could you make the “rule” explicit? How should the algorithm decide which elements to put in which list? – Jim Danner May 02 '20 at 22:23
  • They should be in the same order as the original list, but separated by the value in the third column. – suso May 02 '20 at 22:26
  • Does this answer your question? [Group list by values](https://stackoverflow.com/questions/5695208/group-list-by-values) – DYZ May 02 '20 at 23:08

1 Answers1

0
  1. You can iterate over list.
  2. Now check if your filter element is present inside list.
for l in list:
    if filter_element in l:
        filtered_list1.append(l)
    elif condition2:
        filtered_list2.append(l)

If you want to do this more aesthetically use can use filter from functools.

Anmol Batra
  • 159
  • 1
  • 8