How to get all values from python enum class? How to get all values from python enum class? python python

How to get all values from python enum class?


You can do the following:

[e.value for e in Color]


You can use IntEnum:

from enum import IntEnumclass Color(IntEnum):   RED = 1   BLUE = 2print(int(Color.RED))   # prints 1

To get list of the ints:

enum_list = list(map(int, Color))print(enum_list) # prints [1, 2]


Based on the answer by @Jeff, refactored to use a classmethod so that you can reuse the same code for any of your enums:

from enum import Enumclass ExtendedEnum(Enum):    @classmethod    def list(cls):        return list(map(lambda c: c.value, cls))class OperationType(ExtendedEnum):    CREATE = 'CREATE'    STATUS = 'STATUS'    EXPAND = 'EXPAND'    DELETE = 'DELETE'print(OperationType.list())

Produces:

['CREATE', 'STATUS', 'EXPAND', 'DELETE']