×

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.

Related Topics

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...

3 minutes read.

Python Program to Print Sum of all Elements in an Array

Python program to print sum of all elements in an array A set of objects stored in contiguous memory locations is referred to as an array. The concept is to keep...

2 minutes read.

Python Indentation

Overview The spaces at the start of a code line are known as an indentation in Python programming. Indentation is only used for readability in other programming languages like C, C++,...

8 minutes read.

AES CTR Python

Python Programming Language  Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a...

3 minutes read.

Index Error in Python

The index errors are the run time error that is raised in Python when we try to access an index that does not exist. This might seem very trivial but...

4 minutes read.

Cross Validation in Sklearn

Data scientists can benefit from cross-validation in machine learning in two key ways: it can assist in minimising the amount of data needed and ensure the artificial intelligence model is...

14 minutes read.

PyPi TensorFlow

It is a free open-source library for high-performative numerical computations. The architecture of TensorFlow allows us for easy deployment and computing across a varied number of platforms and from desktops...

3 minutes read.

Convert uppercase to lowercase in Python

Python String lower() By using the built-in string lower() method, all the uppercase characters can be converted into lowercase characters in a string, The lowercased string from the given string is returned...

1 minute read.

Python enumerate() function

Python enumerate() function The enumerate() function in Python takes a collection (e.g. a tuple) and returns it as an enumerate object. Syntax: enumerate(iterable, start=0) Parameter Iterable:  This parameter represents an iterable object start:    This parameter represents a number defining...

1 minute read.

Python min() function

Python min() function returns you the minimum value element in an iterable. We can also use this function to determine the smallest value among various arguments passed to this function. We...

4 minutes read.

Excel Automation with Python

Data analysis is the upcoming technology in the IT sector; this data analysis can be performed easily with the help of data frames or by using excel sheets. The data...

4 minutes read.

XGBoost for Regression in Python

Regression problem results real values. Decision Trees and Linear Regression are regularly used regression algorithms and use some metrics involved in regression like mean squared error and root mean squared...

5 minutes read.

Collections in python

In this tutorial, we will see what is collection in python.  Further, we will see in-depth the different kinds of collections in python. Collections Collections in python are the built-in module used...

9 minutes read.

Python Stack

Python Stack: The work Stack is defined as arranging a pile of objects or items on top of another. It is the same method of allocating memory in the stack...

10 minutes read.

How to create a login page in python

Users can access an application by providing their username and Password or by authenticating with a social media login on the login screen. Python Tkinter Python provides a variety of choices for...

3 minutes read.

Python any() Function

Python any() Function The any() function in Python returns a boolean value ‘True’ if any element of the iterable is true or if the iterable is empty, else it returns False. Syntax any(iterable) Parameter Iterable: An iterable object (list, tuple, dictionary) Return This...

1 minute read.

Python List Size

Introduction The list data type in Python is an ordered, flexible collection. A list may also contain duplicate entries. To get the size of any object, use the len() function in...

6 minutes read.

Assertion error in python

In this article, we will discuss assertions in the python programming language. Assertion: Assertions are a way of telling a program to test a certain condition and trigger an error if the...

3 minutes read.

Python program to print array element present at even position

Python program to print array element present at even position In this program, we'll see a Python program that prints the elements of an array that are in even positions. We...

1 minute read.

Python hash() function

Python hash() function The hash() function in Python returns the hash value of the object (if it has one).  Syntax hash(object) Parameter obj : This parameter represents the object which we need to convert into hash. Return This...

1 minute read.