×

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 Set clear() Method

Python Set clear() Method The set.clear() method removes all the elements from the set. Syntax set.clear() Parameter NA Return None Example 1 # Python program explaining # the set.clear() method # initializing the set fruits = {"watermelon", "banana", "apple"} # printing the set...

2 minutes read.

Paramiko Python Example

Python Programming Language Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

6 minutes read.

Python List count() method

Python List count() method The list.count() method returns the number of times x appears in the list. Syntax list.count(x) Parameter x: This parameter represents the value to search for and can contain any iterable (list, set, tuple, etc.) Return This method returns the...

1 minute read.

Captcha Code in Python with Example

Python Programming Language Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

6 minutes read.

XXhash Python Examples

XXhash Python: xxHash is an extremely rapid hash calculation that operates inside the confines of RAM. Code is incredibly convenient, and hashes (almost nothing/large endian) are same at all levels. Execution of...

4 minutes read.

Convert List to Array in Python

What is List in Python? In Python, a list is a built-in data structure that stores a collection of items. The items in a list can be of any data type,...

3 minutes read.

How to Open a File in Python with a Path?

File in Python File handling is an important operation; it allows the programmer to access the data in the files, such as text, binary values and excel sheets. The CSV files...

7 minutes read.

How to run a Program in Python

How to run a Program in Python Writing a program in Python is an easy task, beginners who are ready to kickstart their career in the world of programming can create...

3 minutes read.

Python Slice from Last Occurrence of K

Introduction We already know that in Python, cutting produces a sub-string out of a string. The variables start, stop, and step are used to set the slicing range. When dealing on...

3 minutes read.

How to Make an App with Python

In this age of mobiles, rapid mobile application development has got a lot of traction. Moreover, app developers are high in demand because of the increase in digitization. Generally, Python is...

4 minutes read.

Python OOPs: Class, Object, Inheritance and Constructor

Basic Object-Oriented Programming (OOP) Concepts in Python Python is similar to most general-purpose programming languages with an Object-Oriented environment system since its existence. Being an Object-Oriented Programming language, Python provides ease...

9 minutes read.

Python SciPy Library

Introduction SciPy, a logical library for Python is an open-source, BSD-authorized library for arithmetic, science, and design. The SciPy library relies upon NumPy, which gives advantageous and quick N-dimensional exhibit control....

6 minutes read.

CatPlot in Python

Python Seaborn Library Seaborn is a superb Python tool for displaying graphical statistics graphing. Seaborn provides different color schemes and attractive default styles to facilitate the creation of various statistics charts...

8 minutes read.

What does the if __name__ == "__main__" do in Python

In this article, you will learn about the If__name__==__main__ is a statement in python to define modules and the names of the modules. This statement plays a vital role in...

3 minutes read.

Python String isspace() method

Python String isspace() method The string.isspace() method returns a Boolean value true if there are only whitespace characters in the given string. This function is used to check if the given...

2 minutes read.

Python Interface

When creating an application, it is important to continuously keep track of its changes. As an application grows, sometimes it gets hard to manage its updates and changes. Often, you...

4 minutes read.

Python Not Equal Operator

Python provides us with many operators to make tasks easier. There are about 7 categories of operators in Python. One of the 7 classifications is the comparison operators. Just as...

3 minutes read.

Python System Requirements

Introduction As we know, Python is a popular programming language usually used to write scripts for operating systems. It’s handy adequate for utilizing in web development and application design. In this article,...

2 minutes read.

Working with JSON in Python

Python is an Object-Oriented high-level language. Python is designed to be highly beginner-friendly. Python has an English-like syntax, which is very easy to read. In this article, we are going...

4 minutes read.

Python len() function

Python len() function The len() function in Python  returns the number of items in an object. Syntax len(s) Parameter s: This parameter represents a sequence (such as a string, bytes, tuple, list, or range) or...

1 minute read.