Python enum get all values | Example code

Use the IntEnum or loop through it to enum get all values in Python.

Example Get all values from an enum class

Simple example program to get all values from an enum class. You have to also use the list and map function in this example.

from enum import IntEnum


class Country(IntEnum):
    Afghanistan = 93
    Albania = 355
    Algeria = 213


country_codes = list(map(int, Country))
print(country_codes)

Output:

Python enum get all values

Another example

Uusing list comprehension.

from enum import Enum


class Color(Enum):
    RED = 1
    BLUE = 2


res = [e.value for e in Color]
print(res)

Output: [1, 2]

Do comment if you have any doubts and suggestions on this Python enum topic,

Note: IDE: PyCharm 2021.1.3 (Community Edition)

Windows 10

Python 3.7

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.