Python Enum Iteration: Loop Over Members
Iterate over Python Enum members, access names and values, control iteration order, filter and transform members, and handle alias edge cases.

When you define an Enum in Python, you often need to iterate over its members. This article shows how to loop over members, access names and values, control iteration order, and avoid common mistakes.
What Is an Enum in Python?
An Enum is a class that defines a fixed set of named constants. Each member has a name and a value. Python's enum module provides the Enum base class, which you subclass to create your own enumerations.
from enum import Enum
class Color(Enum):
RED = 1
GREEN = 2
BLUE = 3Here, Color.RED is a member with name 'RED' and value 1. The members are instances of the Color class, and they are also attributes of the class.
Iterating Over Enum Members
The simplest way to iterate over all members of an enum is to use a for loop directly on the class. The Color class is iterable because EnumMeta, the metaclass used by Enum, provides __iter__; iteration yields members in definition order.
for color in Color:
print(color)Output:
Color.RED
Color.GREEN
Color.BLUEEach color is an enum member. You can use it anywhere you would use a member, such as in comparisons or as a dictionary key.
If you need a list of all members, you can pass the class to list():
colors = list(Color)The resulting list contains the members Color.RED, Color.GREEN, and Color.BLUE in definition order, which is also the iteration order.
Accessing Name and Value During Iteration
While iterating, you often need the member's name or value. Each enum member has two attributes: .name and .value.
for color in Color:
print(f"{color.name} = {color.value}")Output:
RED = 1
GREEN = 2
BLUE = 3The direct attribute access is clear and fast.
Iteration Order and How to Control It
By default, enum members are iterated in the order they were defined. This order is exposed in __members__, an ordered mapping from member names to members.
for name, member in Color.__members__.items():
print(f"{name} = {member.value}")This gives the same output as before. If you need a different order, you can sort the members based on a key. For example, to iterate by value:
for member in sorted(Color, key=lambda m: m.value):
print(member)If your enum values are not naturally sortable, you can provide a custom key. Keep in mind that sorting adds overhead, so only do it when the order matters for your logic.
Filtering and Transforming Enum Members
Iteration pairs well with comprehensions. You can filter members based on their name or value, or create a mapping from values to members.
# Filter members with even values
even_members = [m for m in Color if m.value % 2 == 0]
# Create a dict mapping values to members
value_to_member = {m.value: m for m in Color}The resulting dict can be used for fast value-to-member lookups. For example, if you receive a numeric value from an external system, value_to_member.get(value) is a safe way to convert it to an enum member without raising an exception.
Performance and Memory Considerations
Iterating over an enum is O(n) in the number of members, which is negligible for typical enums with a handful of entries. Each member is a singleton, so repeated access returns the same object rather than allocating a new one.
If you need to perform many lookups by value, building a dictionary once and reusing it is more efficient than scanning the enum each time. This is especially true if the enum has many members or the lookup happens inside a hot loop.
# Build once
lookup = {m.value: m for m in Color}
# Use many times
color = lookup.get(2)This avoids repeated iteration and makes the intent clearer. However, do not pre-optimize: for most applications, direct iteration is perfectly fine.
Common Mistakes and Edge Cases
One common mistake is assuming that enum members are ordered by value. They are not; they are ordered by definition. If your values are not in the same order as the definitions, iterating directly will not produce a value-sorted sequence.
Another edge case involves aliases. If you define two names with the same value, Python treats the second name as an alias and does not include it in iteration.
class Status(Enum):
ACTIVE = 1
ENABLED = 1 # alias for ACTIVE
INACTIVE = 2
for status in Status:
print(status)Output:
Status.ACTIVE
Status.INACTIVEStatus.ENABLED is still accessible, but it points to the same member as Status.ACTIVE, so normal iteration skips it. If you need to process alias names too, iterate over Status.__members__.items() or Status.__members__.keys() instead; those include every defined name, including aliases.
Using Enum Iteration in Real-World Code
A practical use case is generating a list of choices for a form or API. For example, you might have an enum of user roles and need to present them in a dropdown.
from enum import Enum
class Role(Enum):
ADMIN = 'admin'
EDITOR = 'editor'
VIEWER = 'viewer'
# Build a list of (value, label) tuples for a dropdown
choices = [(role.value, role.name.title()) for role in Role]This produces [('admin', 'Admin'), ('editor', 'Editor'), ('viewer', 'Viewer')]. The iteration is straightforward and the resulting list can be passed directly to a web framework's choice field.
Another common pattern is validating that an input string matches a member name. You can iterate and compare, but a more efficient approach is to build a set of valid names once and reuse it:
valid_names = {member.name for member in Role}
if input_name in valid_names:
role = Role[input_name]Here, input_name is the string you want to validate. Building the set once (for example, at module level) makes membership checks average O(1).
When you work with enums in Python, iteration is a fundamental tool. Understanding how iteration behaves, how to control order, and how aliases affect it will help you write more predictable and maintainable code.
Try matching browser tools
All toolsSmall utilities that fit Python work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related Python guides
All Python guidesPython iter(): How the Built-in Function Works
Explains Python's iter() built-in: how it creates iterators, the two-argument sentinel form, custom iterator classes, and common edge cases.
Python Pika: Queues, Exchanges, Routing Keys, and Acknowledgements
Learn how to use Python's Pika client with RabbitMQ: declare queues and exchanges, publish with routing keys, consume messages, and handle acknowledgements reliably.
Python Kafka: kafka-python vs Confluent Client
python kafka python vs confluent kafka: Compare kafka-python and Confluent Kafka Python clients: API differences, performance, feature coverage, and when to choose eac...
Python Kafka Offsets, Partitions, and Manual Commits
Learn how Kafka offsets and partitions work and how to manually commit offsets in Python consumers with kafka-python and confluent-kafka. Covers sync vs async commits, rebalances, and seeking.
More in Python
All Python guidesThe guides filed next to this one in Python, in library order.