Enumerate() Function in Python
The enumerate() function in Python is used to convert a data collection object into an enumerate object. It returns an object that contains a counter as a key for each value within an object, making items within the collection easier to access. It is basically created with the help of the classes. It has the name as well as values with it. It adds the counter to the iteration and gives you the enumeration object. Enum is collection of the symbolic name that should be unique. It should have the constant value.
Properties of enumerate() function
- It can be displayed as the string.
- We can check that the given data is enum or not using the type() function.
- For display the values, we can use the name keyword.
Syntax:
enumerate(iterable, start=0)
- iterable: It is an object that supports the iteration.
- Start: It specifies the starting position. From that position, it will start the iteration.
Example:
R = ['Yash', 'Raj', 'Rohit']
U = enumerate(R)
# convert enumerate object into the list
print(list(U))
Output:
[(0, 'Yash'), (1, 'Raj'), (2, 'Rohit')]
Explanation:
- Here, we have the list which is R.
- We have added some name into it.
- We have the enumerate object which is U.
- In that object, we have stored the list in the form of object.
- Then, we have converted the object into the list with the help of the list keyword and we have printed the list.
Enum member
The variables or members which are present in the enum class are called as the enum memebers.
Example:
from enum import Enum
class P(Enum):
Yashraj = 1
Rohit = 2
Robert = 3
Widdo = 4
# printingenum member
print(P.Yashraj)
Output:
P.Yashraj
Explanation:
- Here we have class named P which is having enum type.
- Then, we have added some member in it.
- Then, with the help of print function, we have printed the enum members.
Name keyword
With the help of the name keyword, we can print or show the name of the member of enum class.
Example:
from enum import Enum
class P(Enum):
Yashraj = 1
Rohit = 2
Robert = 3
Widdo = 4
Akash = 5
Sumit = 6
Ved = 7
Deepa = 8
# printing name of member using "name" keyword
print(P.Yashraj.name)
Output:
Yashraj
Explanation:
- Here we have class named P which is having enum type.
- Then, we have added some member in it.
- Then with the help of name keyword, we have printed the name of the member which is present in the enum class.
Value keyword
With the help of the value keyword,, we can print the value of the member of enum class.
Example:
from enum import Enum
class P(Enum):
Yashraj = 1
Rohit = 2
Robert = 3
Widdo = 4
Akash = 5
Sumit = 6
Ved = 7
Deepa = 8
# printing value of member using "value" keyword
print(P.Widdo.value)
Output:
4
Explanation:
o Here we have a class named P which is having enum type.
o Then, we have added some member in it.
o Then,, with the help of Value keyword, we have printed the Value of the member which is present in the enum class.
Type keyword
With the help of the type keyword, we can easily find which type of data it is.
Example:
from enum import Enum
class P(Enum):
Yashraj = 1
Rohit = 2
Robert = 3
Widdo = 4
Akash = 5
Sumit = 6
Ved = 7
Deepa = 8
# printing the type of the member with the help of type() function
print(type(P.Rohit))
Output:
<enum 'P'>
Explanation:
- Here we have class named P which is having enum type.
- Then, we have added some member in it.
- Then with the help of type function, we have printed the type of the member which is present in the enum class.
Printing all members using list
Here we are printing the all members of the enum class using the list.
Example:
from enum import Enum
class P(Enum):
Yashraj = 1
Rohit = 2
Robert = 3
Widdo = 4
Akash = 5
Sumit = 6
Ved = 7
Deepa = 8
# printing all memebers ofenunm class using the list
print(list(P))
Output:
[<P.Yashraj: 1>, <P.Rohit: 2>, <P.Robert: 3>, <P.Widdo: 4>, <P.Akash: 5>, <P.Sumit: 6>, <P.Ved: 7>, <P.Deepa: 8>]
Explanation:
- Here we have class named P which is having enum type.
- Then we have added some member in it.
- Then we have converted all class into list and we have printed the list.