close

How to get all values from python enum class?

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to get all values from python enum class in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How to get all values from python enum class?

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

    You can use IntEnum:
    from enum import IntEnum class Color(IntEnum): RED = 1 BLUE = 2 print(int(Color.RED)) # prints 1

  2. get all values from python enum class

    You can use IntEnum:
    from enum import IntEnum class Color(IntEnum): RED = 1 BLUE = 2 print(int(Color.RED)) # prints 1

Method 1

You can do the following:

[e.value for e in Color]

Method 2

You can use IntEnum:

from enum import IntEnum

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


print(int(Color.RED))   # prints 1

To get list of the ints:

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

Conclusion

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read