To get the values from a Python enum, iterate over the enum class and read each member's .value attribute, or build a list with a comprehension: [member.value for member in MyEnum]. If you only need the raw values as a plain list, that single line is the standard answer. The rest of this guide covers the variations you will actually hit: aliases, integer enums, string enums, and the mistakes that make the list come out wrong.
What You'll Need
- Python 3.4 or newer (the
enummodule is in the standard library from 3.4 onward; no install needed). - A code editor or an interactive Python shell.
- An enum class already defined, or a few lines to define one for testing.
Step-by-Step
- Import the enum module and define your enum. The
Enumbase class is what you subclass. A minimal example:
The names on the left are the members; the values on the right are what you want to extract.from enum import Enum class Color(Enum): RED = 1 GREEN = 2 BLUE = 3 - Iterate over the enum class directly. Enum classes are iterable and yield their members in definition order:
This printsfor color in Color: print(color.name, color.value)RED 1,GREEN 2,BLUE 3. Iterating the class is the canonical way to walk members; it skips aliases automatically (see Common Mistakes). - Collect just the values into a list. Use a list comprehension on the member objects:
This is the line most people are looking for. It works for any enum, includingvalues = [color.value for color in Color] print(values) # [1, 2, 3]IntEnumandStrEnum. - Get the names too, if you need them. The same pattern with
.namegives you the string labels:
Pair them withnames = [color.name for color in Color] print(names) # ['RED', 'GREEN', 'BLUE']zipif you want a name-to-value mapping:dict(zip(names, values)). - Look up a single member by value. Call the enum class with the value:
Color(2)returnsColor.GREEN. If the value does not exist, Python raisesValueError, so wrap it in a try/except when the input comes from a user or a file. - Handle integer and string enums. If you defined
class Status(IntEnum)orclass Role(StrEnum), the same iteration and comprehension work. The only difference is that each member is also anintorstr, soStatus.ACTIVE == 1is true and you can pass members directly to functions that expect the primitive type. - Convert to a plain list of primitives when passing to JSON or a database. A list of enum members is not JSON-serialisable; a list of their
.valueattributes is. Use the comprehension from step 3 before callingjson.dumps. - Check your result. Print the list and confirm the length matches the number of members you defined. If it is shorter, you have aliases; if it is longer, you have accidentally included a member from a mixin or a subclass.
Common Mistakes
- Using
Color.__members__and expecting only unique values.__members__is a mapping that includes aliases, so two names can point to the same value. Iterating the class itself (for c in Color) skips aliases; iteratingColor.__members__.values()does not. - Calling
.valueon the class instead of a member.Color.valueraisesAttributeErrorbecausevalueis an instance attribute. You needColor.RED.valueor a loop variable. - Assuming enum values are strings. By default they are whatever you assigned. If you need string values, either assign strings explicitly or use
StrEnum(Python 3.11+); do not rely onstr(member), which returns something likeColor.RED, notRED. - Forgetting that duplicate values become aliases. If two names share a value, only the first is a canonical member; the second is an alias and will not appear when you iterate the class. Use
@uniquefrom theenummodule to catch this at definition time. - Trying to mutate the enum after definition. Enum members are fixed at class creation; you cannot add or remove members at runtime. Build a new enum or use a plain dictionary if the set of values is dynamic.
Tips
- Use
list(Color)to get the member objects, then map.valueonly when you need primitives. Keeping members around preserves type safety. - For a dictionary of name to value,
{c.name: c.value for c in Color}is clearer thanColor.__members__and excludes aliases. - If you are on Python 3.11 or newer,
StrEnummakes string-valued enums behave like strings in comparisons and formatting, which avoids a lot of.valuenoise. - Name your enum class in singular form (
Color, notColors) so the plural list variable reads naturally. - Keep enums at module level, not inside functions, so they can be imported and compared across files.
Troubleshooting
- You get
TypeError: 'Color' object is not iterable. This happens if you iterate an instance rather than the class. Usefor c in Color, notfor c in Color.RED. - Your list is missing members. Check for duplicate values (aliases). Run
Color.__members__and compare its length withlen(list(Color)); a difference means aliases exist. ValueErrorwhen looking up by value. The value you passed is not defined. Confirm the exact type:Color('1')fails if the enum stores integers, because'1'is a string. Convert the input first.- JSON serialisation fails. You are passing enum members, not values. Convert with the comprehension from step 3 before serialising.
AttributeError: value. You called.valueon the class or on a plain string. Confirm you are working with a member object.
Frequently Asked Questions
How do I get all values from an enum as a list?
Use [member.value for member in MyEnum]. Iterating the class yields members in definition order and skips aliases.
What is the difference between __members__ and iterating the class?
__members__ is a mapping of every name, including aliases, to its member. Iterating the class yields only canonical members, so aliases are excluded.
Can I get the value by name?
Yes: MyEnum['RED'].value looks up a member by name and returns its value. Use MyEnum.RED.value when the name is known at write time.
Do enums work with IntEnum and StrEnum?
Yes. The same iteration and .value access apply. The difference is that members also behave as int or str in comparisons and formatting.
Why does my enum have fewer members than I defined?
Duplicate values create aliases. Only the first name with a given value is a canonical member; the rest are aliases and are skipped during iteration. Use @unique to catch duplicates early.
Can I change an enum's values at runtime?
No. Enum members are fixed when the class is created. If your set of values changes at runtime, use a dictionary or build a new enum class instead.