Python Forum
Help: list comprehension for loop with double if statement
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help: list comprehension for loop with double if statement
#1
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?

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 iterable
Thanks!
Reply
#2
data = {'caseA': ['Printers'], 'caseB': None, 'caseC': ['Printers', 'Computers'], 'caseD': None, 'caseE': None}
item = 'Printers'

result = ['affected' if item in (value or ()) else 'unaffected' for value in data.values()]
print(', '.join(result))
of course, it could also be oneliner.
Further reading: https://docs.python.org/3/library/stdtyp...and-or-not
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(May-04-2020, 06:22 AM)buran Wrote:
data = {'caseA': ['Printers'], 'caseB': None, 'caseC': ['Printers', 'Computers'], 'caseD': None, 'caseE': None}
item = 'Printers'

result = ['affected' if item in (value or ()) else 'unaffected' for value in data.values()]
print(', '.join(result))
of course, it could also be oneliner.
Further reading: https://docs.python.org/3/library/stdtyp...and-or-not

Thanks buran for your swift reply!

Small addition, is there any way to get the name of the dictionary key in the result?
I mean....

Output:
caseA affected, caseB unaffected, caseC affected, caseD unaffected, caseE unaffected
Reply
#4
result = [f'{case} affected' if item in (value or ()) else f'{case} unaffected' for case, value in data.items()]
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  List comprehension and Lambda cametan 2 219 Jun-08-2021, 08:29 AM
Last Post: cametan
  Compare each element of an array in a logic statement without using a for loop leocsmith 3 499 Apr-01-2021, 07:57 PM
Last Post: deanhystad
  how to create pythonic codes including for loop and if statement? aupres 1 434 Jan-02-2021, 06:10 AM
Last Post: Gribouillis
  What is the difference between a generator and a list comprehension? Pedroski55 2 385 Jan-02-2021, 04:24 AM
Last Post: Pedroski55
  For Loop with List Comprehension muzikman 25 1,185 Dec-18-2020, 10:45 PM
Last Post: muzikman
  Remove double quotes from the list ? PythonDev 22 1,737 Nov-05-2020, 04:53 PM
Last Post: snippsat
  Using recursion instead of for loops / list comprehension Drone4four 4 612 Oct-10-2020, 05:53 AM
Last Post: ndc85430
  if statement in for loop researcher123 6 482 Oct-01-2020, 05:07 PM
Last Post: deanhystad
  Appending to list of list in For loop nico_mnbl 2 504 Sep-25-2020, 04:09 PM
Last Post: nico_mnbl
  Uses cases for list comprehension (encountered while doing PyBite #77) Drone4four 3 772 Sep-25-2020, 12:14 PM
Last Post: ndc85430

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020