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

Last updated: Feb 18, 2023
3 min

## #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
```Copied!```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
``````

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
```Copied!```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')
``````

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

main.py
```Copied!```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
```Copied!```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')
``````

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
```Copied!```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')
``````