Python enum: get a list of all values
Codemia Knowledge Hub ·
To get all values from a Python Enum, iterate over its members and take .value. The result keeps declaration order and works for validation, serialization, dropdown options and anything else that needs the underlying values rather than the enum objects.
List all values
Names vs values
Each member has a .name and a .value:
Use .name for the symbolic identifiers and .value for the stored values. Mixing the two up often leaks into API design, where one part of the system expects names and another expects values.
Getting the members themselves
If later code needs both sides, keep the members instead of converting to values too early:
Name-to-member mapping with __members__
__members__ is an ordered mapping of names to members:
It is useful when you need the names and the enum objects in one place.
Names and values together for an API
For UI or schema generation, a list of dictionaries saves downstream code from joining names and values later, and is easy to serialize or send to frontend code:
Iteration follows declaration order
Enum iteration preserves the order in which members were defined, so the enum definition can also serve as the source of display or validation order for the rest of the application.
Duplicate values and aliases
If several names share a value, the extra names are aliases:
Default iteration over Example skips aliases, so "all values" depends on whether alias members should count. If they matter, inspect __members__ instead.
Mistakes when extracting enum values
- Confusing names with values. Decide up front whether the consumer needs
.name,.valueor the member itself. - Expecting aliases in the iteration. When duplicate values exist, iterating the enum does not return every alias.
- Converting to values too early. If later code needs the members (for type checks or to read
.name), passlist(MyEnum)and extract values at the last moment.
Quick reference:
[member.value for member in MyEnum]gives all values.[member.name for member in MyEnum]gives the symbolic names.list(MyEnum)gives the members.MyEnum.__members__gives the ordered name-to-member mapping, including aliases.
Related reading
- How to get allocated GPU spec in Google Colab
- How to get an absolute file path in Python
- how to get basicproperties header field in pika python from rabbitmq messages?
- How to get contents of a text file from AWS s3 using a lambda function?
- how to get covariance matrix in tensorflow?
- How to get current available GPUs in tensorflow?
- How to get current TensorFlow name scope
- How to get current time in python and break up into year, month, day, hour, minute?
System design interview problems on this topic
- Design a Web Crawler: solution and walkthrough
- Design a Task Scheduler: solution and walkthrough
- Design a URL Shortener (TinyURL): solution and walkthrough
- Design an API Rate Limiter: solution and walkthrough
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.