- [PYTHON] How to check if a value exists in an enum
- Isn’t Python constant in the first place?
- Enumeration notation
- Enums and loops
- List type
- Enum
- I think I can write if I come here
- bonus
- Postscript
- Python enum check contains
- # Table of Contents
- # Check if a value exists in an Enum in Python
- # Checking if a value is NOT in an Enum
- # Check if a name exists in an Enum in Python
- # Check if a name exists in an Enum using the __members__ attribute
- # Additional Resources
- How To Check If A Value Exists In Python Enum
- Check if a value exists in an Enum in Python
- Use ‘value’ attribute
- Use the __member__ attribute
- Summary
[PYTHON] How to check if a value exists in an enum
You want to use a constant when you want to have a value for a specific thing. When dealing with constants in Python, I personally think it’s better to use ʻEnum` (enumeration type).
Isn’t Python constant in the first place?
That’s right. There is no strict constant, as any value can be overwritten depending on how you do it. But the official document says so.
Enumeration notation
from enum import Enum class Hoge(Enum): ONE = 1 TWO = 2 THREE = 3
Enums and loops
Since the enumeration type is a set of identifiers, I think there are few cases where the value itself given to the identifier is meaningful. If it is a list type, it can be easily output with a for statement, but if it is an enumeration type, it requires some ingenuity in writing.
List type
>>> lst = [1, 2, 3] >>> [l for l in lst] [1, 2, 3]
Enum
>>> from enum import Enum >>> class Hoge(Enum): . ONE = 1 . TWO = 2 . THREE = 3 . >>> [v.value for n, v in Hoge.__members__.items()] [1, 2, 3]
I think I can write if I come here
>>> from enum import Enum >>> class Hoge(Enum): . ONE = 1 . TWO = 2 . THREE = 3 . >>> [v.value for n, v in Hoge.__members__.items()] [1, 2, 3] >>> 1 in [v.value for n, v in Hoge.__members__.items()] True
It’s done. I don’t know where to use it.
bonus
What’s inside the enumerated __members__ ?
>>> Hoge.__members__ mappingproxy(< 'ONE': , 'TWO': , 'THREE': >)
What is MappingProxyType ? According to the official documentation
Read-only mapping proxy. Provides a dynamic view of mapping entries. This means that if the mapping changes, the view will reflect these changes.
I see(?) There is ʻitems ()` in this proxy. It looks like a dictionary when you take a quick look.
Postscript
I was told that there is a simpler way of writing than @shiracamus.
>>> 1 in [e.value for e in Hoge] True
>>> any(e.value == 1 for e in Hoge) True
It seems that you can write. Thank you! !! The first one is the level of why I didn’t notice .
I can get advice by writing an article, so I thought that output is important.
Python enum check contains
Last updated: Feb 18, 2023
Reading time · 3 min
# Table of Contents
# Check if a value exists in an Enum in Python
To check if a value exists in an enum in Python:
- Use a list comprehension to get a list of all of the enum’s values.
- Use the in operator to check if the value is present in the list.
- The in operator will return True if the value is in the list.
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 .
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.
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:
- Use a list comprehension to get a list of all of the enum’s names.
- Use the in operator to check if the name is present in the list.
- The in operator will return True if the name is in the list.
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.
Copied!from enum import Enum class Sizes(Enum): SMALL = 1 MEDIUM = 2 LARGE = 3 # 👇️ , 'MEDIUM': , 'LARGE': > print(Sizes.__members__) if 'SMALL' in Sizes.__members__: # 👇️ this runs print('SMALL is in enum names')
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.
How To Check If A Value Exists In Python Enum
To check if a value exists in an Enum in Python, you can use the ‘value’ and the __ member __ attribute. Follow the article to better understand.
Check if a value exists in an Enum in Python
Enum stands for enumeration, which is enumeration in Python. The point of Enum is that it is advantageous when representing data representing a finite set. The Enums class generates the enumeration. An enumeration consists of names and data linked together.
from enum import Enum class FootballClubs(Enum): ManchesterUnited = 1 Liverpool = 2 ManchesterCity = 3 NottinghamForest = 4 # Printing enum member as a string print(FootballClubs.ManchesterUnited) # Use the "name" keyword to print out the name of the enum member print(FootballClubs.ManchesterUnited.name) # Use the "name" keyword to print out the value of the enum member print(FootballClubs.ManchesterUnited.value)
To check if a value exists in an Enum in Python. You can use the following ways:
Use ‘value’ attribute
- Import the module named “enum “.
- The Enums class generates enumerations. In this example, I created an enumeration named ‘FootballClubs’.
- Using the ‘value’ attribute to get the values in the ‘FootballClubs’ enum
- Use the in operator to check if a value exists in an Enum.
from enum import Enum class FootballClubs(Enum): ManchesterUnited = 1 Liverpool = 2 ManchesterCity = 3 NottinghamForest = 4 # Using the 'value' attribute to get the values in the 'FootballClubs' enum valuesClub = [club.value for club in FootballClubs] # The in operator checks if a value exists in an Enum print(1 in valuesClub) print(7 in valuesClub)
The value 1 is in the Enum, so it returns True. The value 7 is not in the Enum, so it returns False.
Use the __member__ attribute
- Import the module named “enum “.
- The Enums class generates enumerations. In this example, I created an enumeration named ‘FootballClubs’.
- Use the in operator with the _member_ attribute to check if a name exists in an Enum.
from enum import Enum class FootballClubs(Enum): ManchesterUnited = 1 Liverpool = 2 ManchesterCity = 3 NottinghamForest = 4 # Using the in operator with the _member_ attribute to check if a value exists in an Enum print('ManchesterUnited' in FootballClubs.__members__) print('Liverpool' in FootballClubs.__members__) print('RealMadrid' in FootballClubs.__members__)
Summary
Here are the methods that can help you check if a value exists in an Enum in Python. If you have any questions about this article, leave a comment below. I will answer your questions. Thank you for reading!
Maybe you are interested:
My name is Jason Wilson, you can call me Jason. My major is information technology, and I am proficient in C++, Python, and Java. I hope my writings are useful to you while you study programming languages.
Name of the university: HHAU
Major: IT
Programming Languages: C++, Python, Java