akromx
akromx

Reputation: 99

TypeError: Argument of type 'NoneType' is not iterable in List comprehension

I have a list comprehension like this : view['entries'] = [entry for entry in view['entries'] if qs in entry.get('name') or qs in entry.get('description')]

This list comprehension is a search filter that works with the name field because it's a required field that is never empty. But when I search for description, I got the error TypeError: argument of type 'NoneType' is not iterable

Description field can be empty.

Upvotes: 1

Views: 164

Answers (1)

user2390182
user2390182

Reputation: 73450

If in doubt, you can always just provide an empty string default value:

if qs in entry.get('name', '') or qs in entry.get('description', ''):

If the keys can be actually present with value None, you can do:

if qs in (entry.get('name') or '') or qs in (entry.get('description') or ''):

Upvotes: 2

Related Questions