python


Pythonic way to subtract one list from another with validation of presence


A good pythonic way to subtract one list from another is to use list comprehension:
# remove all elements of list2 from list1
list1 = [x for x in list1 if x not in list2]
How would I, in the same step, validate that each x actually occurs in list1 (e.g. in order to raise an error on missing elements)?
You don't need to check. x by definition has to be in list1, or else the iterator wouldn't provide it (since you're iterating over list1's items)
But in case it might get modified while you're iterating (which is a case you should definitely avoid):
list1 = [x for x in list1 if x not in list2 and x in list1]
If you need more complex behaviour like showing an error message, use an explicit loop
list_delta = []
for x in list1:
if x not in list1:
raise IndexError("oh god what")
if x not in list2:
list_delta.append(x)
But honestly this seems like an X-Y problem because you should never have to do this to begin with.
I'm not sure I understand what you're asking there, but if the order doesn't matter and there are no repetitions, you can use set difference as #Chih-Hsu Jack Lin mentioned in the comments:
list(set(list1) - set(list2))
And if order doesn't matter, but element count does matter, another approach is to use collections.Counter:
list((Counter(list1) - Counter(list2)).elements())

Related Links

Sql Alchemy What is wrong?
How do I plot a graph in Python?
Reordering matrix elements to reflect column and row clustering in naiive python
making urllib request in Python from the client side
Grid within a frame?
how to create http headers from scratch
Mapping a range of values to another
maya2008 win32api 64 bit python
Python importing modules differently when run interactively/non-interactively
Why do I get this error in this Python code?
what is the correct way to close a socket in python 2.6?
Listing blog entries by year,month
Extract URLs out of email in Python
Packet Queue in Python?
Looping through files in a folder
Control decimal division precision in MySQLdb

Categories

HOME
laravel-5
ipython
mapbox-gl-js
ll
bro
httpclient
currency
flann
tee
dlib
jacoco
line-api
arraylist
derived
jpeg2000
styles
jive
jconsole
plsqldeveloper
apple-tv
dspic
qsub
hanami
dss
vcard
pentaho-report-designer
dd-wrt
crt
lmdb
android-vpn-service
oracle-xml-db
xen
deepstream.io
rule
html-agility-pack
amd
nsjsonserialization
ticker
tcserver
forerunnerdb
instruction-set
mercury
unity3d-editor
iron.io
spyne
libreadline
freetype2
distribute
proxygen
brackets-shell
uialertview
viper-architecture
pgm
sqoop2
google-earth-plugin
start-job
dto
eclipse-classpath
boost-test
level
ifft
broadcasting
anjuta
jquery-tabs
onselect
execvp
surveyor-gem
cadisplaylink
scrollto
concurrent-programming
path-manipulation
webkit-transform
opengl-es-lighting
ihttphandler
nvelocity
carbide
visual-studio-2010-beta-2
soft-keyboard
castle-validators
cots

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