python


How to subset a list with list comprehension using another list


I have a large list of dictionaries I want to subset, which looks like this:
first_list = [{'name':'James','gender':'M','address':'California'},{'name':'Tom','gender':'M','address':'California'},
{'name':'Jane','gender':'F','address':'Utah'},
{'name':'Kim','gender':'F','address':'Wisconsin'},
{'name':'Ron','gender':'M','address':'Montana'}]
I have another list, with names:
second_list = ['James', 'Tom']
I want to get the list where 'name' in the first list is not a part of the second list, which is simply removing James and Tom dictionaries.
[{'name':'Jane','gender':'F','address':'Utah'},
{'name':'Kim','gender':'F','address':'Wisconsin'},
{'name':'Ron','gender':'M','address':'Montana'}]
I tried using list comprehension, but I don't think this works with different lists:
third_list = [x for x in first_list if x['name'] != (y for y in second_list)]
This won't work, will return the same list as the first list. Is my syntax wrong?
Use not in that will work for you.
third_list = [i for i in first_list if i['name'] not in second_list]
Result
[{'address': 'Utah', 'gender': 'F', 'name': 'Jane'},
{'address': 'Wisconsin', 'gender': 'F', 'name': 'Kim'},
{'address': 'Montana', 'gender': 'M', 'name': 'Ron'}]
Your code doesn't work , because (y for y in second_list) is a generator, x['name'] is a string , which means it will return False all the time, I suppose what you want is this:
>>> first_list = [{'name':'James','gender':'M','address':'California'},{'name':'Tom','gender':'M','address':'California'},
... {'name':'Jane','gender':'F','address':'Utah'},
... {'name':'Kim','gender':'F','address':'Wisconsin'},
... {'name':'Ron','gender':'M','address':'Montana'}]
>>>
>>>
>>> second_list = ['James', 'Tom']
>>>
>>> [x for x in first_list if x['name'] not in second_list]
[{'gender': 'F', 'name': 'Jane', 'address': 'Utah'}, {'gender': 'F', 'name': 'Kim', 'address': 'Wisconsin'}, {'gender': 'M', 'name': 'Ron', 'address': 'Montana'}]
Pythonic way to do this , try to use filter method:
>>> filter(lambda x:x["name"] not in second_list,first_list)
[{'gender': 'F', 'name': 'Jane', 'address': 'Utah'}, {'gender': 'F', 'name': 'Kim', 'address': 'Wisconsin'}, {'gender': 'M', 'name': 'Ron', 'address': 'Montana'}]
Use
third_list = [x for x in first_list if x['name'] not in second_list]

Related Links

Removing repeated element from a list [duplicate]
py.test -k: collecting tests takes too much time
Uploading Django projects set up within virtual environment on Github
Conditioning and operation on multidimensional (3D) array in Python-NumPy
Python regex alternative for join
SQLAlchemy 'character_set_name' error in GAE
How to backup database using XMLRPC?
Convert a list of ints to a float
Select file to upload using Internet Explorer in python
Better way to parse from regex?
python nested lists - list comprehension of dictionaries
Can Install Packages but cannot import
Inserting lines to a file after a string match
All possible combinations of dictionary values given input string. Python
Assign and compare in python
List comprehension won't return expected output

Categories

HOME
sql-server
cil
numpy
ssas-2012
answer-set-programming
textwatcher
amazon-product-api
playframework
h2
apache2
hyperledger-fabric
branch.io
currency
ipfs
oclint
lagom
xul
abcpdf
eclipse-luna
medium-editor
sql-tuning
riak-ts
uiautomator
ghost-inspector
facebook4j
richfaces
iup
go-cd
memsql
novnc
pdfsharp
geopandas
net-snmp
continuous-deployment
guile
interrupt-handling
widevine
helper
mayavi
data-conversion
vertex-buffer
positioning
xor
fractals
shapes
ocs
honeysql
sidr
mms
pmwiki
swagger-editor
jquery-multidatespicker
alchemy.js
linq-to-entities
code-composer
bing-translator-api
excel-interop
worker
asmx
onmouseover
data-integration
forerunnerdb
volume
vst
update-site
gameanalytics
tuxedo
multiple-file-upload
linkageerror
pagekit
soda
string-parsing
logcat
git-rebase
asymptote
hateoas
windows-vista
registrykey
windowlistener
gray-code
rspec3
swift2.1
subversion-edge
sysfs
google-earth-plugin
lexicographic
orientation-changes
kallithea
utf-32
jstat
emma
crystal-reports-10
flash-cc
circos
bignum
java.util.date
installshield-2011
javascriptmvc
vmware-server
google-closure-library
mailcore
database-permissions
objcmongodb
marmalade-edk
continuous-testing
xmlslurper
isolatedstorage
msgbox
zookeeper
ou
tcxgrid
printing-web-page
paintcomponent
zend-form-element
aio
linfu
castle-validators
gears

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App