Getting Names and Values of an Enum in Python

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
4 min

banner

# Table of Contents

  1. Get Enum name by value in Python
  2. Get a List of all Enum Values or Names in Python
  3. Getting Enum Names and Values in Python

# Get Enum name by value in Python

To get an enum name by value, pass the value to the enumeration class and access the name attribute, e.g. Sizes(1).name.

When the value is passed to the class, we get access to the corresponding enum member, on which we can access the name attribute.

main.py
from enum import Enum class Sizes(Enum): SMALL = 1 MEDIUM = 2 LARGE = 3 print(Sizes(1).name) # ๐Ÿ‘‰๏ธ SMALL print(Sizes(2).name) # ๐Ÿ‘‰๏ธ MEDIUM print(Sizes(3).name) # ๐Ÿ‘‰๏ธ LARGE

get enum name by value

The code for this article is available on GitHub

The Sizes(1) syntax allows us to pass an integer to the class and get the corresponding enum member.

main.py
from enum import Enum class Sizes(Enum): SMALL = 1 MEDIUM = 2 LARGE = 3 value = 1 print(Sizes(value)) # ๐Ÿ‘‰๏ธ Sizes.SMALL print(Sizes(value).name) # ๐Ÿ‘‰๏ธ SMALL
This is useful when you don't know the name of the enum member ahead of time (because it's read from a file or fetched from an API).

# Get a List of all Enum Values or Names in Python

Use a list comprehension to get a list of all of an enum's values or names.

On each iteration, access the value or name attributes on the enum member to get a list of all of the enum's values or names.

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] names = [member.name for member in Sizes] print(names) # ๐Ÿ‘‰๏ธ ['SMALL', 'MEDIUM', 'LARGE']

get list of all enum values or names

The code for this article is available on GitHub

On each iteration, we access the value attribute to get a list of all of the enum's values.

Similarly, we can access the name attribute on each iteration to get a list of all of an enum's names.

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

You can use the same approach if you need to get a list of tuples containing the name and value of each enum member.

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

The first element in each tuple is the name, and the second is the value of the enum member.

Use the in operator if you need to check if a value is in an enum.

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 values')
The code for this article is available on GitHub

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.

You can use a simple for loop if you need to iterate over an enum.

main.py
from enum import Enum class Sizes(Enum): SMALL = 1 MEDIUM = 2 LARGE = 3 for size in Sizes: print(size) print(size.name, size.value)

# Getting Enum Names and Values in Python

To access an enum's names and values:

  1. Use dot notation to access a specific enum member, e.g. Sizes.SMALL.
  2. Use the name and value properties to access the enum's names and values.
main.py
from enum import Enum class Sizes(Enum): SMALL = 1 MEDIUM = 2 LARGE = 3 # โœ… Get the Enum's name print(Sizes.SMALL.name) # ๐Ÿ‘‰๏ธ SMALL # ๐Ÿ‘‡๏ธ access enum name by value print(Sizes(1).name) # ๐Ÿ‘‰๏ธ SMALL print(Sizes['SMALL'].name) # ๐Ÿ‘‰๏ธ SMALL # --------------------------------- # โœ… Get the Enum's value print(Sizes.SMALL.value) # ๐Ÿ‘‰๏ธ 1 print(Sizes['SMALL'].value) # ๐Ÿ‘‰๏ธ 1

getting enum names and values

The code for this article is available on GitHub

As shown in the code sample, you can use the name and value properties on an enum member to get the enum's name and value.

main.py
from enum import Enum class Sizes(Enum): SMALL = 1 MEDIUM = 2 LARGE = 3 print(Sizes.MEDIUM.name) # ๐Ÿ‘‰๏ธ MEDIUM print(Sizes.MEDIUM.value) # ๐Ÿ‘‰๏ธ 2

You can also use square brackets to access enum members.

main.py
from enum import Enum class Sizes(Enum): SMALL = 1 MEDIUM = 2 LARGE = 3 name = 'LARGE' print(Sizes[name].name) # ๐Ÿ‘‰๏ธ MEDIUM print(Sizes[name].value) # ๐Ÿ‘‰๏ธ 2
This is useful when you don't know the name of the enum member ahead of time (because it's read from a file or fetched from an API).

If you only have the value that corresponds to the enum member, pass it to the enumeration class and access the name attribute.

main.py
from enum import Enum class Sizes(Enum): SMALL = 1 MEDIUM = 2 LARGE = 3 print(Sizes(1).name) # ๐Ÿ‘‰๏ธ SMALL print(Sizes(2).name) # ๐Ÿ‘‰๏ธ MEDIUM print(Sizes(3).name) # ๐Ÿ‘‰๏ธ LARGE
The code for this article is available on GitHub

This is only necessary when you need to get the name of an enum member by its value.

# 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