Skip to main content

How to Check for Value or Name Membership in Python Enums

Python's Enum class provides a way to create symbolic names for constant values.

This guide explains how to check if a specific value or name exists within an Enum, using both list comprehensions and the more direct in operator on the Enum itself.

Checking if a Value Exists in an Enum

The most straightforward and Pythonic way to check if a value exists in an Enum is to use the in operator directly with a list of the Enum's values.

from enum import Enum

class Sizes(Enum):
SMALL = 1
MEDIUM = 2
LARGE = 3

values = [member.value for member in Sizes]
if 2 in values:
print('2 is in enum values') # Output: 2 is in enum values
else:
print("Value not found")
print(100 in values) # Output: False
  • Using a list comprehension extracts all enum values.
  • The in operator then checks for membership in the list of extracted values.

Using a List Comprehension (Less Efficient)

You could technically create a list comprehension and check for value existence:

from enum import Enum

class Sizes(Enum):
SMALL = 1
MEDIUM = 2
LARGE = 3

values = [member.value for member in Sizes]
print(values) # Output: [1, 2, 3]

if 2 in values: # Check the values list
print('2 is in enum values')
else:
print("Value is not found")
  • You first create a list using a list comprehension, iterating over the Enum members, and extracting the value for each.
  • The in keyword is used to check if the list has the value you are looking for.
  • Disadvantage: This approach is less efficient than the direct in test, as it requires creating a temporary list of the enum's values.

Checking if a Name Exists in an Enum

To check if a particular name (the string representation of the enum member, e.g., 'SMALL') exists within an Enum, you have two main options.

The best approach is to use the in operator directly with the __members__ attribute of the Enum class:

from enum import Enum
class Sizes(Enum):
SMALL = 1
MEDIUM = 2
LARGE = 3

print(Sizes.__members__)
# Output: {'SMALL': <Sizes.SMALL: 1>, 'MEDIUM': <Sizes.MEDIUM: 2>, 'LARGE': <Sizes.LARGE: 3>}

if 'SMALL' in Sizes.__members__:
print('SMALL is in enum names') # Output: SMALL is in enum names
else:
print("Name not found")
  • Sizes.__members__: This is a read-only, ordered mapping (similar to a dictionary) that maps the names of the enum members to the members themselves.
  • in Sizes.__members__: This directly checks if the string 'SMALL' is a key in the __members__ mapping. This is very efficient (like checking for a key in a dictionary).

Using a List Comprehension (Less Efficient)

You could also create a list of names and then use in:

from enum import Enum

class Sizes(Enum):
SMALL = 1
MEDIUM = 2
LARGE = 3
names = [member.name for member in Sizes]
print(names) # Output: ['SMALL', 'MEDIUM', 'LARGE']

if 'SMALL' in names:
print('SMALL is in enum names')
else:
print("Name not found")
  • This approach is less efficient than using the in operator directly on __members__, as it needs to first create a list containing all of the enum values.

Conclusion

This guide presented various methods for checking whether a value or a name exists within a Python Enum. The recommended and most efficient methods are:

  • For value existence: Create a list containing all the values of the enum, and use the in operator to check if the value exists in the list.
  • For name existence: Use the in operator directly on the __members__ attribute of the Enum class (if 'NAME' in MyEnum.__members__:).

These direct checks leverage Python's built-in optimizations for membership testing and are more readable than creating intermediate lists. Avoid list comprehensions for simple membership checks in Enums.