Python enum return all values

Get List of all Enum values in python

This tutorial will discuss about unique ways to get list of all enum values in Python.

Table Of Contents

Get List of all Enum values

Iterate over all entries in Enum in a List Comprehension. For each entry in enum, select its value property during iteration, to get the value field of each entry of enum. The List comprehension will return a list of all values of the Emum.

Let’s see the complete example,

from enum import Enum class ChannelStatus(Enum): IDE =1 ACTIVE = 2 BLOCKED = 3 WAITING = 4 CLOSED = 5 # Get list of all enum values in Python enumValues = [statusMem.value for statusMem in ChannelStatus] print (enumValues)

Frequently Asked:

Get Names of all entries in Enum

Iterate over all entries in Enum using a List Comprehension. During iteration, for each entry in enum, access its name property, to get the name field of that entry in enum. The List comprehension will return a list of all names of all entries in Emum.

Let’s see the complete example,

from enum import Enum class ChannelStatus(Enum): IDE =1 ACTIVE = 2 BLOCKED = 3 WAITING = 4 CLOSED = 5 # Get list of names of all enum values in Python enumValueNames = [statusMem.name for statusMem in ChannelStatus] print (enumValueNames)
['IDE', 'ACTIVE', 'BLOCKED', 'WAITING', 'CLOSED']

Get name/value pairs of all entries in Enum

Iterate over all entries in Enum using for loop, and for each enum entry, access its name and value properties to get the details. In the belowe example, we have printed all the names and value of all entries in the enum, one by one.

Читайте также:  Safe mode для php

Let’s see the complete example,

from enum import Enum class ChannelStatus(Enum): IDE =1 ACTIVE = 2 BLOCKED = 3 WAITING = 4 CLOSED = 5 # iterate over all entries of name, and print # name, value of each entry in enum for status in ChannelStatus: print("Name : ", status.name, " :: Value : ", status.value)
Name : IDE :: Value : 1 Name : ACTIVE :: Value : 2 Name : BLOCKED :: Value : 3 Name : WAITING :: Value : 4 Name : CLOSED :: Value : 5

Summary

We learned about different ways to get a List of all Enum values in Python. Thanks.

Share your love

Leave a Comment Cancel Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Terms of Use

Disclaimer

Copyright © 2023 thisPointer

To provide the best experiences, we and our partners use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us and our partners to process personal data such as browsing behavior or unique IDs on this site and show (non-) personalized ads. Not consenting or withdrawing consent, may adversely affect certain features and functions.

Click below to consent to the above or make granular choices. Your choices will be applied to this site only. You can change your settings at any time, including withdrawing your consent, by using the toggles on the Cookie Policy, or by clicking on the manage consent button at the bottom of the screen.

The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.

The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.

The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.

Читайте также:  Php fpm nginx user

The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.

Источник

Get A List Of All Enum Values In Python

Get A List Of All Enum Values In Python

If you are looking for a tutorial to get a list of all enum values in Python, keep reading our article. It’s helpful for you. Let’s start now!

Get the values of an enum

Enum is a data structure implemented by importing a module. This data structure includes two fields, names and values.

To get a list of all enum values, first, you need to get the values and then represent them under the type list. Each pair name and value is considered as an element of an enum, so you can traverse the enum and access the name or value as an attribute.

Look at the following example that displays how to get an enum’s values.

# Import the module Enum from enum import Enum # Create an Enum class Week(Enum): Monday = 2 Tuesday = 3 Wednesday = 4 Thursday = 5 Friday = 6 Saturday = 7 Sunday = 8 print("Values of the Enum Week are:") for day in Week: print(day.value, end=' ')
Values of the Enum Week are: 2 3 4 5 6 7 8 

You can also get the values through the names.

# Import the module Enum from enum import Enum # Create an Enum class Week(Enum): Monday = 2 Tuesday = 3 Wednesday = 4 Thursday = 5 Friday = 6 Saturday = 7 Sunday = 8 print("Value of Monday is:", Week.Monday.value)

Let’s move on. We will discover how to represent an enum’s values as a list.

Get a list of all enum values in Python

Append each value to the list

As we mentioned above, we can traverse the enum and get the values. So now, we will create an empty list, traverse the enum, get the value, and append it to the list. Finally, we will get the list as our expected result.

# Import the module Enum from enum import Enum # Create an Enum class Week(Enum): Monday = 2 Tuesday = 3 Wednesday = 4 Thursday = 5 Friday = 6 Saturday = 7 Sunday = 8 # Create an empty list myEnum = [] for day in Week: myEnum.append(day.value) print("My list of all Enum values is:",myEnum)
My list of all Enum values is: [2, 3, 4, 5, 6, 7, 8]

Use list comprehension

List comprehension is a technique to create a list with a short syntax. You can create a list only by one line of code, which carries out two functions declaring and traversing. Look at the following example that uses list comprehension techniques.

# Import the module Enum from enum import Enum # Create an Enum class Week(Enum): Monday = 2 Tuesday = 3 Wednesday = 4 Thursday = 5 Friday = 6 Saturday = 7 Sunday = 8 # List comprehension myEnum = [day.value for day in Week] print("My list of all Enum values is:",myEnum)
My list of all Enum values is: [2, 3, 4, 5, 6, 7, 8]

Use map() and lambda()

We had an article explaining the function: map() and lambda(). Please visit here if you have any confusions. In the following example, we will show you how to create list from the enum’s values by the map() and lambda() function.

# Import the module Enum from enum import Enum # Create an Enum class Week(Enum): Monday = 2 Tuesday = 3 Wednesday = 4 Thursday = 5 Friday = 6 Saturday = 7 Sunday = 8 myEnum = list(map(lambda x: x.value, Week)) print("My list of all Enum values is:",myEnum)
My list of all Enum values is: [2, 3, 4, 5, 6, 7, 8]

Summary

Our tutorial has explained how to get a list of all enum values in Python. We hope our article can help you. If you have any questions, please leave us your comments. We will respond as possible. Thank you for your reading!

Читайте также:  Scroll popup css что это

Maybe you are interested:

My name is Robert Collier. I graduated in IT at HUST university. My interest is learning programming languages; my strengths are Python, C, C++, and Machine Learning/Deep Learning/NLP. I will share all the knowledge I have through my articles. Hope you like them.

Name of the university: HUST
Major: IT
Programming Languages: Python, C, C++, Machine Learning/Deep Learning/NLP

Источник

Оцените статью