Get A List Of All Enum Values In Python

Get A List Of All Enum Values In Python

If you are looking for a tutorial to get a list of all enum values in Python, keep reading our article. It’s helpful for you. Let’s start now!

Get the values of an enum

Enum is a data structure implemented by importing a module. This data structure includes two fields, names and values.

To get a list of all enum values, first, you need to get the values and then represent them under the type list. Each pair name and value is considered as an element of an enum, so you can traverse the enum and access the name or value as an attribute. 

Look at the following example that displays how to get an enum’s values.

Code:

# Import the module Enum
from enum import Enum
 
# Create an Enum
class Week(Enum):
  Monday = 2
  Tuesday = 3
  Wednesday = 4
  Thursday = 5
  Friday = 6
  Saturday = 7
  Sunday = 8
  
print("Values of the Enum Week are:")

for day in Week:
  print(day.value, end=' ')

Result:

Values of the Enum Week are:
2 3 4 5 6 7 8 

You can also get the values through the names.

Code:

# Import the module Enum
from enum import Enum
 
# Create an Enum
class Week(Enum):
  Monday = 2
  Tuesday = 3
  Wednesday = 4
  Thursday = 5
  Friday = 6
  Saturday = 7
  Sunday = 8
  
print("Value of Monday is:", Week.Monday.value)

Result:

Value of Monday is: 2

Let’s move on. We will discover how to represent an enum’s values as a list.

Get a list of all enum values in Python

Append each value to the list

As we mentioned above, we can traverse the enum and get the values. So now, we will create an empty list, traverse the enum, get the value, and append it to the list. Finally, we will get the list as our expected result. 

Code:

# Import the module Enum
from enum import Enum
 
# Create an Enum
class Week(Enum):
  Monday = 2
  Tuesday = 3
  Wednesday = 4
  Thursday = 5
  Friday = 6
  Saturday = 7
  Sunday = 8
  
# Create an empty list
myEnum = []
 
for day in Week:
  myEnum.append(day.value)
  
print("My list of all Enum values is:",myEnum)

Result:

My list of all Enum values is: [2, 3, 4, 5, 6, 7, 8]

Use list comprehension

List comprehension is a technique to create a list with a short syntax. You can create a list only by one line of code, which carries out two functions declaring and traversing. Look at the following example that uses list comprehension techniques.

Code:

# Import the module Enum
from enum import Enum
 
# Create an Enum
class Week(Enum):
  Monday = 2
  Tuesday = 3
  Wednesday = 4
  Thursday = 5
  Friday = 6
  Saturday = 7
  Sunday = 8
  
# List comprehension
myEnum = [day.value for day in Week]
 
print("My list of all Enum values is:",myEnum)

Result:

My list of all Enum values is: [2, 3, 4, 5, 6, 7, 8]

Use map() and lambda()

We had an article explaining the function: map() and lambda(). Please visit here if you have any confusions. In the following example, we will show you how to create list from the enum’s values by the map() and lambda() function.

Code:

# Import the module Enum
from enum import Enum
 
# Create an Enum
class Week(Enum):
  Monday = 2
  Tuesday = 3
  Wednesday = 4
  Thursday = 5
  Friday = 6
  Saturday = 7
  Sunday = 8
  
myEnum = list(map(lambda x: x.value, Week))
print("My list of all Enum values is:",myEnum)

Result:

My list of all Enum values is: [2, 3, 4, 5, 6, 7, 8]

Summary

Our tutorial has explained how to get a list of all enum values in Python. We hope our article can help you. If you have any questions, please leave us your comments. We will respond as possible. Thank you for your reading!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *