Okay lets say I have a list, and I want to check if that list exists within another list. I can do that doing this:
all(value in some_map for value in required_values)
Which works fine, but lets say I want to the raise an exception when a required value is missing, with the value that it is missing. How can I do that using list comprehension?
I'm more or less curious, all signs seem to point to no.
EDIT Argh I meant this:
for value in required_values:
if value not in some_map:
raise somecustomException(value)
Looking at those I cant see how I can find the value where the error occurred
lets say i want to the raise an exception when a required value is missing, with the value that it is missing. How can i do that using list comprehension?
List comprehensions are a syntactically concise way to create a list based on some existing list—they're not a general-purpose way of writing any for
-loop in a single line. In this example, you're not actually creating a list, so it doesn't make any sense to use a list comprehension.
You can't use raise in a list comprehension. You can check for yourself by looking at the grammar in the Python Language Reference.
You can however, invoke a function which raises an exception for you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With