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

Python: How can I index in MapReduce(MRJob)?
Download multiple CSV files from a list in a single CSV (Python)
Does Python garbage collect when Heroku warns about memory quota vastly exceeded (R15)?
reading function from another file into a button in python
Python - Mult-Threading Help - Reading Multiple Files - ETL Into SQL Server
'if' statement for when two random numbers are equal
How to read two lines in a data from same column to create combination of values from that column?
Jupyter pyspark : no module named pyspark
How to group data by ranges?
Series to_records - no object
python multiprocessing pool.map not blocking?
Append to zip() in Python
unbound method must be called with instance as first argument (got str instance instead)
pass object primary key across templates django
Sqlite insert result doesn't show on the next immediate select commmand
Change Django Shell Plus Kernel default specs

Categories

HOME
xbox-live
json
visual-studio-2015
google-apps-script
google-tag-manager
smartphone
memory-leaks
search-engine
is-empty
apiconnect
nodemailer
vsftpd
jboss-eap-7
abcpdf
local
simple-injector
equalizer
typeahead
rhapsody
tortoisegit
plsqldeveloper
epicor
url.action
richfaces
pentaho-report-designer
skygear
preg-grep
canvasjs
nsurlconnection
css-counter
oscommerce
parse-android-sdk
elfinder
strstr
concrete5-5.7
android-preferences
onbackpressed
mediawiki-extensions
timesten
range-v3
proof
goquery
nuget-server
pycurl
rxvt
picasa
rstudio-server
ruby-2.0
angularjs-ng-pattern
pax-web
com-interop
logcat
sonarqube5.3
java.util.calendar
coldfusion-7
enyo
visual-studio-code
uptodate
tcpreplay
ultrawingrid
google-admin-audit-api
power-law
data-generation
pic24
zipalign
initialization-vector
circos
bignum
cilk-plus
dllexport
shortcuts
transitive-closure-table
mozart-mvc
dropdownlistfor
maven-ear-plugin
osi
static-variables
cadisplaylink
scrollto
mpmovieplayer
newsstand-kit
easygui
printing-web-page
for-xml-path
nagle
audiostreamer
rijndael
pantheios
openvg
opcodes
system-codedom-compiler
data-entry

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