Join Regular Classroom : Visit ClassroomTech

Python Set – enumerate() Method

It is a method in python that iterates through the set, adds a counter to it and returns an object. The object can be converted to set, list or tuple later.

Syntax:
enumerate(iterable, starting_position)

Also Read: Python Set – any() Function

Example:

# Python code to understand enumerate() method in set
# www.codewindow.in

s = {"code", "sleep", "repeat"}
s1 = {"codewindow"}
ob1 = enumerate(s)
ob2 = enumerate(s1)
print(ob1)
print(set(ob1))
print(set(ob2))

Output:

<enumerate object at 0x2b0e3508c438>
{(2, 'sleep'), (1, 'code'), (0, 'repeat')}
{(0, 'codewindow')}

Explanation:
Here it returned an object, which is later converted to set. Each character of the string is iterated (“code”, “sleep”, “repeat”) and returned with a counter (0, 1, 2, 3..) inside the set as pairs.


Follow Us

You Missed

Also Checkout