0

I have the following list, composed by a list of lists, I would like to take it:

list = [[original1,type1,unknow],[original2,type2,general]]

and to generate the following result, using List Comprehensions, the idea is to verify if the last element is unknow then return the original element if not return the general, for that i need to search with maybe two for's i tried:

desired output:

newList = [origina1,general]

I tried, however i am confusing with the sintaxys of comprenhension lists, i hope someone could support me

newList =[ x if list[3] == 'unknow' else x == general for x in list]
neo33
  • 1,809
  • 5
  • 18
  • 41

1 Answers1

1

I think you meant to get the first element if the last one is unknow and the last element if not:

In [3]: l = [['original1', 'type1', 'unknow'],['original2', 'type2', 'general']]

In [4]: [item[0] if item[-1] == 'unknow' else item[-1] for item in l]
Out[4]: ['original1', 'general']

Or, the "unpacking" version (if the number of items in the sublists is known and it is 3):

In [5]: [a if c == 'unknow' else c for a, _, c in l]
Out[5]: ['original1', 'general']

_ is a canonical way to name throwaway variables.

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • Thanks I really appreciate the support this way is a very efficient and fine way thanks, – neo33 Dec 20 '16 at 18:09