Home > Blockchain >  Python if statement to skip dictionary key when creating a dictionary
Python if statement to skip dictionary key when creating a dictionary

Time:02-13

My code looks like this:

def createEventBody(name,description,attendees,location, eventColor = None):
    eventBody = {
              'Name': name, #EventName
              'Color': eventColor,
              'location': location,
              'description':description,
              'attendees': [],
              }

The thing is, I want to add some logic so that the key 'Color' is not included if eventColor = None. I was thinking something like this:

def createEventBody(name,description,attendees,location, eventColor = None):
    eventBody = {
              'Name': name, #EventName
              ('Color': eventColor) if eventColor != None else pass,
              'location': location,
              'description':description,
              'attendees': [],
              }

However 'pass' doesn't allow me to "skip" that key. How could I fix this?

CodePudding user response:

x if y else z is a conditional expression: it is a way of issuing a value that depends on a condition. It is not suitable for doing something or not doing it. That is what an if statement is for.

Set the Color entry in an if statement after the eventBody dict is created.

eventBody = {
          'Name': name,
          'location': location,
          'description':description,
          'attendees': [],
}
if eventColor is not None:
    eventBody['Color'] = eventColor

is there a way to do this but for multiple variables without the use of multiple if statements? So instead of doing color is not None, description is not None doing it all with one single expression?

Yes. You could set all those entries in your dictionary, and then omit the ones that have the value None.

Something like this:

eventBody = {
          'Name': name,
          'Color': eventColor,
          'location': location,
          'description':description,
          'attendees': [],
}

eventBody = {k:v for (k,v) in eventBody.items() if v is not None}

Or if you only want to filter some specific keys, like this:

for key in ('Color', 'location', 'description'):
    if eventBody[key] is None:
        del eventBody[key]

CodePudding user response:

pass is not an expression, it does not have a value. I would stick with the original (i.e., set 'Color': None), but if you do not want None, you can just do a standard if statement.

CodePudding user response:

You can use dict.update method

   eventBody = {
              'Name': name, #EventName
              'location': location,
              'description':description,
              'attendees': [],
              }
if eventColor:
    eventBody.update({"Color": eventColor})

CodePudding user response:

You can't use if statement inside the dict.

But, this will get your work done.

eventBody = {
      'Name': name,
      'location': location,
      'description':description,
      }
if eventColor:
    eventBody.update(Color=eventColor)
  • Related