Live Chat
True
FAQs
Development

Python enumerate() Function

Example

Convert a tuple into an enumerate object:

x= ["eat", "sleep", "repeat"]
s1 = "geek"

# creating enumerate objects
obj1 = enumerate(x)
obj2 = enumerate(s1)

print ("Return type:", type(obj1))
print (list(enumerate(x)))

# changing start index to 2 from 0
print (list(enumerate(s1, 2)))
-----------------------------------------Output-----------------------------------

Return type: <class 'enumerate'>
[(0, 'eat'), (1, 'sleep'), (2, 'repeat')]
[(2, 'g'), (3, 'e'), (4, 'e'), (5, 'k')]

x= ["eat", "sleep", "repeat"]

# printing the tuples in object directly
for ele in enumerate(x):
print (ele)
--------------Output without determining start point for starting Number to enumerate-------------------

(0, 'eat')
(1, 'sleep')
(2, 'repeat')
-----------------------------but if when using start point for starting Number to enumerate-------------------------------
# changing index and printing separately
for count, ele in enumerate(x, 100):
print (ele)
---------------------output------------
(100, 'eat')
(101, 'sleep')
(102, 'repeat')

-----------------------------but if when using start point for starting Number to enumerate with count the result-------------------------------
for count, ele in enumerate(x, 100):
print (count, ele)
---------------------output------------
100 eat
101 sleep
102 repeat
--------------------------
# getting desired output from tuple
for count, ele in enumerate(x):
print(count)
print(ele)
---------------------output------------
0
eat
1
sleep
2
repeat


Was this article helpful?

FAQ HOME

To install this Web App in your iPhone/iPad press and then Add to Home Screen.