Check if a Value or a Name exists in an Enum in Python

avatar
Borislav Hadzhiev

Last updated: Feb 18, 2023
3 min

banner

# Table of Contents

  1. Check if a value exists in an Enum in Python
  2. Check if a name exists in an Enum in Python

# Check if a value exists in an Enum in Python

To check if a value exists in an enum in Python:

  1. Use a list comprehension to get a list of all of the enum's values.
  2. Use the in operator to check if the value is present in the list.
  3. The in operator will return True if the value is in the list.
main.py
from enum import Enum class Sizes(Enum): SMALL = 1 MEDIUM = 2 LARGE = 3 values = [member.value for member in Sizes] print(values) # ๐Ÿ‘‰๏ธ [1, 2, 3] if 2 in values: # ๐Ÿ‘‡๏ธ this runs print('2 is in enum values') else: print('2 is NOT in enum values') print(100 in values) # ๐Ÿ‘‰๏ธ False

check if value exists in enum

The code for this article is available on GitHub

We used a list comprehension to get a list of the enum's values.

List comprehensions are used to perform some operation for every element, or select a subset of elements that meet a condition.

The last step is to use the in operator to check if a specific value is in the list.

The in operator tests for membership. For example, x in l evaluates to True if x is a member of l, otherwise it evaluates to False.

# Checking if a value is NOT in an Enum

If you need to check if a value is not in an enum, negate the conditional with not.

main.py
from enum import Enum class Sizes(Enum): SMALL = 1 MEDIUM = 2 LARGE = 3 values = [member.value for member in Sizes] print(values) # ๐Ÿ‘‰๏ธ [1, 2, 3] if 100 not in values: # ๐Ÿ‘‡๏ธ This runs print('100 is NOT in enum values')

check if value is not in enum

The code for this article is available on GitHub

You can use the same approach if you need to check if a specific name is present in an enum.

main.py
from enum import Enum class Sizes(Enum): SMALL = 1 MEDIUM = 2 LARGE = 3 names = [member.name for member in Sizes] print(names) # ๐Ÿ‘‰๏ธ ['SMALL', 'MEDIUM', 'LARGE'] if 'SMALL' in names: # ๐Ÿ‘‡๏ธ this runs print('SMALL is in enum names')

Instead of accessing the value attribute, we used the name attribute to get a list of the enum's names.

# Check if a name exists in an Enum in Python

To check if a name exists in an enum in Python:

  1. Use a list comprehension to get a list of all of the enum's names.
  2. Use the in operator to check if the name is present in the list.
  3. The in operator will return True if the name is in the list.
main.py
from enum import Enum class Sizes(Enum): SMALL = 1 MEDIUM = 2 LARGE = 3 names = [member.name for member in Sizes] print(names) # ๐Ÿ‘‰๏ธ ['SMALL', 'MEDIUM', 'LARGE'] if 'SMALL' in names: # ๐Ÿ‘‡๏ธ this runs print('SMALL is in enum names')

check if name exists in enum

The code for this article is available on GitHub

We used a list comprehension to get a list of the enum's names.

List comprehensions are used to perform some operation for every element, or select a subset of elements that meet a condition.

The last step is to use the in operator to check if a specific name is in the list.

The in operator tests for membership. For example, x in l evaluates to True if x is a member of l, otherwise it evaluates to False.

# Check if a name exists in an Enum using the __members__ attribute

Alternatively, you could use the __members__ attribute on the class.

main.py
from enum import Enum class Sizes(Enum): SMALL = 1 MEDIUM = 2 LARGE = 3 # ๐Ÿ‘‡๏ธ {'SMALL': <Sizes.SMALL: 1>, 'MEDIUM': <Sizes.MEDIUM: 2>, 'LARGE': <Sizes.LARGE: 3>} print(Sizes.__members__) if 'SMALL' in Sizes.__members__: # ๐Ÿ‘‡๏ธ this runs print('SMALL is in enum names')

check if name exists in enum using members attribute

The code for this article is available on GitHub

The __members__ attribute is an ordered mapping of names to members.

It contains all names defined in the enum (including the aliases).

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev