Home > Enterprise >  Bypassing NoneType in a list comprehension
Bypassing NoneType in a list comprehension

Time:05-21

To work around when there is no value x.competition.name, I tried to use is not None:

'competition': [x.competition.name if x.competition.name is not None else '-' for x in a]

But the error still keeps showing up:

AttributeError: 'NoneType' object has no attribute 'name'

How can I go about getting around this problem?

CodePudding user response:

It sounds like you need to test x.competition:

'competition': [x.competition.name if x.competition is not None else '-' for x in a]

CodePudding user response:

Apparently competition is None, so please replace

[x.competition.name if x.competition.name is not None else '-' for x in a]

using

[x.competition.name if x.competition is not None else '-' for x in a]

CodePudding user response:

The error message is saying that you tried to get None.name. That means x.competition must be None, which is why you can't get a name attribute from it. Instead, try making your condition x.competition is not None.

  • Related