0

Here's an example of what I want to achieve:

LIST = [{
    'name': 'A',
    'travel experience': 'YES',
},
{
    'name': 'E',
    'travel experience': 'YES',
},
{
    'name': 'F',
    'travel experience': 'NO',
},
{
    'name': 'G',
    'travel experience': 'YES',
    }
]

I want to get a new list whose travel experience is 'YES' only. So, the output that I want to get is:

NEW_LIST = [{
    'name': 'A',
    'travel experience': 'YES',
},
{
    'name': 'E',
    'travel experience': 'YES',
},
{
    'name': 'G',
    'travel experience': 'YES',
    }
]

Please help me figure this out.

I've tried slicing: a = LIST[:2] + LIST[3]

That didn't work because of the list[3]

a = LIST[:2]
b = LIST[3]
c = a.update(b)

That didn't work either because of a is a list.

Nick
  • 81
  • 5
  • If you know the index of the dict that you want to remove, why not just do `del LIST[2]`? – Aran-Fey May 02 '19 at 09:05
  • Anyway, `b = LIST[3:]` is what you want. – Aran-Fey May 02 '19 at 09:09
  • Oh that's right! The list is a constant though. So, I can solve this by just making a copy of the list and go from there correct? – Nick May 02 '19 at 09:12
  • Yes. That's more efficient than concatenating two slices, too. – Aran-Fey May 02 '19 at 09:14
  • What if the list contains 7 names with travel experience values in the following orders: YES, YES, NO, YES, NO, YES, NO and I just want to get a new list with people who only have travel experience? What is the most efficient way to do this? – Nick May 02 '19 at 09:19
  • 1
    [python filter list of dictionaries based on key value](//stackoverflow.com/q/29051573) – Aran-Fey May 02 '19 at 09:20
  • Thanks a million! :) – Nick May 02 '19 at 09:55

0 Answers0