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

convert element and next element in list of strings to date in python
Python Type Hints: Specifying a type to be a List of numbers (ints and/or floats)?
PyQT4: how to add item/subitem to QtTreewidget, remove item through buttons and get the values of subitems
Creating a “snake” counter
Upgrade Python 3.4.3 to Python 3.6.1 in Ubuntu 15.04(64 bit)
Unable to call a method within a schedule job
Python ModuleNotFoundError during gunicorn start
How to add ModelChoice field of django form in template
selenium css_selector struggle
How to sort the data by a keyword of a csv file in Python?
mouse move in python video game
Tensorflow Sampled Softmax Loss Correct Usage
Set cbar min and max for seaborn clustermap
Angle between planes algorithm is too slow
Python - Using .readlines() with .rstrip() and then store all words into a list
Python Decorator validation

Categories

HOME
paramiko
google-sheets-api
apache2
schemacrawler
pygame
flexbox
jboss-eap-7
ndis
gatsby
onsen-ui2
ctypes
mongodb-query
scala-native
arm-template
derived
boolean-expression
rails-activerecord
epicor
npm-install
csh
java-ee-7
cep
unpack
xilinx-ise
configure
positioning
vegan
svnkit
nodeclipse
xamarin.uitest
axis-labels
wsadmin
web-development-server
excel-interop
walmart-electrode
google-maps-ios
jszip
mercury
gulp-typescript
libtiff.net
ebtables
uistackview
except
asymptote
jta
sem
response-headers
webhdfs
stream-framework
isml
apache-mina
jazz
uptodate
robocode
gmaps4rails
collabnet
node-imagemagick
csplit
config-files
winrt-httpclient
hamsterdb
onselect
backbone-relational
client-side-scripting
arbor.js
phpsh
cgpath
dropdownlistfor
pitch
autostart
static-variables
fotoware
caliper
wcf-web-api
separation-of-concerns
trampolines
document-library





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