May-04-2020, 06:12 AM
Hi,
I have the following piece if code which I want to write more cleanly using list comprehension.
However, I am struggling with the list comprehension using the double if statement in the for loop.
Can somebody help me with this?
I have the following piece if code which I want to write more cleanly using list comprehension.
However, I am struggling with the list comprehension using the double if statement in the for loop.
Can somebody help me with this?
data = {'caseA': ['Printers'], 'caseB': None, 'caseC': ['Printers', 'Computers'], 'caseD': None, 'caseE': None}
item = 'Printers'
relations = ''
if data:
for case in data:
if data[case] is not None:
if item in data[case]:
label = 'affected'
else:
label = 'unaffected'
else:
label = 'unaffected'
relations = relations + label + ','I have this:if data: label = ['unaffected' if data[case] is None else 'affected' for case in data if item in data[case]]but it results in an error related to 'None':
Error:label = ['unaffected' if data[case] is None else 'affected' for case in data if current_element in data[case]]
TypeError: argument of type 'NoneType' is not iterableThanks!
