This tutorial will discuss about unique ways to get list of all enum values in Python.
Table Of Contents
Get List of all Enum values
Iterate over all entries in Enum in a List Comprehension. For each entry in enum, select its value
property during iteration, to get the value field of each entry of enum. The List comprehension will return a list of all values of the Emum.
Let’s see the complete example,
from enum import Enum class ChannelStatus(Enum): IDE =1 ACTIVE = 2 BLOCKED = 3 WAITING = 4 CLOSED = 5 # Get list of all enum values in Python enumValues = [statusMem.value for statusMem in ChannelStatus] print (enumValues)
Output
[1, 2, 3, 4, 5]
Get Names of all entries in Enum
Iterate over all entries in Enum using a List Comprehension. During iteration, for each entry in enum, access its name
property, to get the name field of that entry in enum. The List comprehension will return a list of all names of all entries in Emum.
Let’s see the complete example,
Frequently Asked:
- Remove a specific column from a List of lists in Python
- Get sublist from List based on indices in Python
- Check for Duplicates in a List in Python
- How to create a List from 1 to 100 in Python?
from enum import Enum class ChannelStatus(Enum): IDE =1 ACTIVE = 2 BLOCKED = 3 WAITING = 4 CLOSED = 5 # Get list of names of all enum values in Python enumValueNames = [statusMem.name for statusMem in ChannelStatus] print (enumValueNames)
Output
['IDE', 'ACTIVE', 'BLOCKED', 'WAITING', 'CLOSED']
Get name/value pairs of all entries in Enum
Iterate over all entries in Enum using for loop, and for each enum entry, access its name
and value
properties to get the details. In the belowe example, we have printed all the names and value of all entries in the enum, one by one.
Let’s see the complete example,
from enum import Enum class ChannelStatus(Enum): IDE =1 ACTIVE = 2 BLOCKED = 3 WAITING = 4 CLOSED = 5 # iterate over all entries of name, and print # name, value of each entry in enum for status in ChannelStatus: print("Name : ", status.name, " :: Value : ", status.value)
Output
Name : IDE :: Value : 1 Name : ACTIVE :: Value : 2 Name : BLOCKED :: Value : 3 Name : WAITING :: Value : 4 Name : CLOSED :: Value : 5
Summary
We learned about different ways to get a List of all Enum values in Python. Thanks.