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

finding email address in a web page using regular expression
Python code giving error
Celery concurrency configuration for io/cpu bound task
Function that takes 3 list arguments and returns all the combinations [duplicate]
how to read files with special characters in python
Using the Python Request Library to get Google Oauth2 tokens
(Beginner)Python functions Codeacademy
Python script hangs
How to shift a list nth time in Left or Right and fill it up with 0 in python
NumPy, why equality check does not work for an array of objects?
String formatting of timedeltas in Pandas
Celery - How to send task from remote machine?
YouTube Api v3 handling Exceptions in Python
Copy data from one oracle database to another with Python
converting python program into executable
How to use multidatabase across multiproject in django?

Categories

HOME
ajax
sidekiq
symfony
mql4
puzzle
filterrific
blockchain
laravel-5.3
decorator
hana
csvhelper
search-engine
adsense
netflix-feign
powershell-v3.0
x11
ctypes
game-physics
cosmicmind
rebol2
datagrip
mule-studio
python-unicode
pyephem
propel2
uiautomator
os161
cocoa-touch
k2
csh
net-snmp
branch
hash-collision
pptp
vcard
bitcoin-testnet
datadog
autofill
bxslider
outsystems
ios-ui-automation
large-data
ksoap
redhat-datavirt
zero
axis-labels
initializer
silverlight-3.0
agent
equivalence
wcf-security
service-fabric-stateful
gd
uiviewpropertyanimator
istorage
ogre3d
slot
httplib2
opencpu
vici
svn-merge
typhoon
galleriffic
rstudio-server
metalsmith
proxygen
eml
altbeacon
data-import
js-cookie
windowlistener
qbfc
anti-patterns
ipojo
calibration
viper-architecture
adobe-indesign
candidate-key
scala-2.11
firebug-lite
humanizer
neoload
facebook-chat
entity-framework-4.1
justgage
symphony-cms
awesomeprint
abnf
attachevent
enumerators
moq-3
vim-powerline
feof
nsindexpath
qtembedded
opengl-es-lighting
pureftpd
surefire
visual-c++-2008-express
act
system-codedom-compiler
web-analytics-tools

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