Reputation: 5726
I just recently found myself writing this line of code, which i did not like too much:
if ('upload' in request.POST) or ('delete' in request.POST):
I allready thought about list comprehension, which would look like this:
if [value for value in ['upload','delete'] if value in request.POST]:
Which is not exactly better. My very simple question is: can this be simplified? Or is this just trying to be too smart?
Upvotes: 0
Views: 228
Reputation: 799580
Simplification uses any()
.
if any(value for value in ['upload','delete'] if value in request.POST):
Upvotes: 1
Reputation: 839254
You could write it more concisely by using set intersection:
if {'upload', 'delete'} & set(request.POST):
Or more explicitly:
if {'upload', 'delete'}.intersection(request.POST):
Upvotes: 6