Skip to content

Instantly share code, notes, and snippets.

@nigelbabu
Created September 3, 2013 07:02
Show Gist options
  • Save nigelbabu/6420526 to your computer and use it in GitHub Desktop.
Save nigelbabu/6420526 to your computer and use it in GitHub Desktop.
# with lambda
pkg_dict['resources'] = filter(lambda x: x.get('id') != id, pkg_dict.get('resources', []))
#without lambda
for res in pkg_dict.get('resources', []):
if res.get('id') == id:
pkg_dict['resources'].remove(id)
@nibrahim
Copy link

nibrahim commented Sep 3, 2013

Can't you do something like

pkg_dict['resources'] = [x.get('id') for x in pkg_dict.get('resources', []) if x.get('id') != id]

@mihi-tr
Copy link

mihi-tr commented Sep 3, 2013

Like the filter option better ;) - since id is the key can't you convert this to a dict and then access by id?

Note only works if you don't have multiple resources per id (don't know your data)

pd=dict((x.get('id'),x) for x in pkg_dict.get('resources',[]))
return pd.get(id)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment