×

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

Python SymPy

A Python package for symbolic mathematics is called SymPy. Its goal is to develop into a fully-fledged computer algebra system (CAS) while maintaining the code as straightforward as possible to...

3 minutes read.

Anaconda python 3 installation for windows 10

If you are a problem solver and like to solve programming questions, the anaconda distribution for python could be one of the best options to use and solve problems in...

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

Python Set isdisjoint() method

Python Set isdisjoint() method The set.isdisjoint() method in Python returns a boolean value True if two sets are disjoint sets ( i.e. none of the elements are present in both sets), otherwise it returns...

1 minute read.

Python Letter to Number

Python Letter to Number In this tutorial, we will convert the given letters into numbers using a Python. We will convert the given letter into the letter value as defined in...

3 minutes read.

Python AIOHTTP

Python 3.5 introduced some new syntax that makes it simpler for developers to make asynchronous programmes and packages. Aiohttp, an HTTP client/server for asyncio, is one such package. In essence,...

3 minutes read.

Python pow() Function

Python pow() Function The pow() function in Python return the parameter ‘x’ to the power ‘y’ and if the parameter ‘z’ is present, it returns x to the power y, modulo z (computed more efficiently than pow(x, y) % z). Syntax pow(x, y[, z]) Parameter x: This parameter represents the base...

1 minute read.

Check Palindrome in Python

Python is an object-oriented high-level programming language. Python has dynamic semantics and has high-level built-in data structures which support dynamic typing and dynamic binding. Python provides rapid development. It has...

4 minutes read.

Struct Module in Python

What is a structure in Python? An entity that holds data in form of a structure is called a data structure. In Python, the data structure includes tuples, lists, dictionaries, and...

4 minutes read.

Python Dictionary copy() method

Python Dictionary copy() method The dictionary.copy () method in Python returns a copy of the specified dictionary. Syntax dictionary.copy () Parameter NA Return None Example 1 # Python program explaining # the dictionary.copy() method # initialising the dictionary ...

1 minute read.

Data Drop in Python

Introduction You'll understand how to delete a group of rows from a Pandas dataframe in this article.You can read this article on How to Drop Columns in Pandas to find out...

6 minutes read.

Comment starts with the symbol in 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.

How to learn python Online

Need to Learn a Programming Language 1. To get a high Paying job We know that Software Engineering is one of the top-paying professions in the world. The top criteria to be...

3 minutes read.

Python String zfill() method

Python String zfill() method The string.zfill() method in Python returns a copy of the string while adding the zeros (0) at the beginning of the string, until it reaches the specified...

1 minute read.

Python Lexicographic Order

Python lexicographic order Before we discuss the lexicographic order in Python, we should understandwhat is lexicographic order and sort according to lexicographic order. Lexicographic order In mathematics, the generalization of the alphabetical order...

5 minutes read.

BOTTLE Python Web Framework

The Bottle is a lightweight WSGI micro web framework for python. It acts like a thin wrapper around a web server where it is distributed as a single file module...

4 minutes read.

Expressions in Python

What is Expression in Python? The expression contains more than one operator as well as the operands with it. Expression helps us to produce some other values. In the Python programming...

12 minutes read.

Python String splitlines() method

Python String splitlines() method The string.splitlines() method in Python splits the specified string and returns a list of the lines in the string, breaking at line boundaries.  Syntax splitlines([keepends]) Parameter keepends(optional): This parameter specifies if...

1 minute read.

Flutter with tensor flow in python

Python : Python is an object oriented programming language which is highly interpreted and is highly interactive. Python was created by Guido van Rossum in the year 1985 – 1990 .The...

3 minutes read.

How to reverse a string in python

How to reverse a string in python A Brief About Strings- The String is a data type in Python that has a sequence of characters. This series of characters are represented in...

4 minutes read.