Skip to content

Enumerate Function in Python

Enumerate Function in Python

Enumerate function in Python creates tuple taking counter of the element and the element and returns a enumerate-object containing all the tuples . If we typecast the enumerate-object into the list, then each element of the list will be a tuple.

Syntax of Enumerate function in Python

enumerate(iterable, start)

Parameters of Enumerate function in Python

  • iterable = iterable must be a sequence, an iterator , or some other object which supports iteration .
  • start = start is an integer number which specifies from which number counting will start . default value is 0 .

Return type of Enumerate function in Python

It returns an enumerate-object .

Enumerate function in Python Examples

Example 1: Creating a enumerate object from string

myString="Rajkumar"

# creating a enumerate object
myEnumerate=enumerate(myString)

# printing the enumerate object
print(myEnumerate)

# converting the enumerate object to a list
print(list(myEnumerate))
<enumerate object at 0x0000023E6B046A80>
[(0, 'R'), (1, 'a'), (2, 'j'), (3, 'k'), (4, 'u'), (5, 'm'), (6, 'a'), (7, 'r')]
  • In the above code first we have declared myString as "Rajkumar"
  • Then we have applied enumerate() function to the string . As default value of start parameter is 0 so counting will start from 0 .
  • Then enumerate function will create multiple tuple and return a enumerate-object containing all the tuple
  • First tuple will be (0,"R") , second tuple will be (1,"a") and the process goes on until the string ends .
  • Then we have printed the enumerate-object . After that we have printed the enumerate object again converting it into a list . We can see that in the printed list all the element is a tuple .

Example 2: Using start parameter

# enumerate functiomn with custom start
myString="Rajkumar"

# creating a enumerate object
myEnumerate=enumerate(myString,100)

# converting the enumerate object to a list
print(list(myEnumerate))
[(100, 'R'), (101, 'a'), (102, 'j'), (103, 'k'), (104, 'u'), (105, 'm'), (106, 'a'), (107, 'r')]

In the above code we have used start parameter as 100 . So counting has been start from 100 .

Iterating an enumerate object

We can also iterate an enumerate-object using for-loop and while-loop .

Example 1: Iterate enumerate object using for loop

# iterating an enumerate-object with for loop
myString="Rajkumar"

for i in enumerate(myString):
    print(i)
(0, 'R')
(1, 'a')
(2, 'j')
(3, 'k')
(4, 'u')
(5, 'm')
(6, 'a')
(7, 'r')

Example 2: Iterate enumerate object using for loop

# iterating an enumerate-object with for loop
myString="Rajkumar"

for (counter,character) in enumerate(myString):
    print("counter is",counter,"and character is",character)
counter is 0 and character is R
counter is 1 and character is a
counter is 2 and character is j
counter is 3 and character is k
counter is 4 and character is u
counter is 5 and character is m
counter is 6 and character is a
counter is 7 and character is r

In the above two code we have iterated enumerate-object using for-loop .

Example 3: Iterate enumerate object using while loop

# iterating an enumerate-object with while loop
myString="Rajkumar"

# we need to convert enumerate object to a list
# because we need to use len() in while-loop condition
# but enumerate-object does not support len()
myEnumerate=list(enumerate(myString))

i=0
while i<len(myEnumerate):
    print(myEnumerate[i])
    i+=1
(0, 'R')
(1, 'a')
(2, 'j')
(3, 'k')
(4, 'u')
(5, 'm')
(6, 'a')
(7, 'r')

In the above code we have iterated enumerate-object using while-loop .

Converting enumerate-object to different datatype

Here we will see how to convert enumerate-object into different datatype( such as –list ,tuple etc) with examples-

Example 1:

myString="Rajkumar"

# converting to list using list() function
print("converted list =",list(enumerate(myString)))

# converting to tuple using tuple() function
print("converted tuple =",tuple(enumerate(myString)))

# converting to dictionary using dict() function
print("converted dictionary =",dict(enumerate(myString)))

# converting to set using set() function
print("converted set =",set(enumerate(myString)))
converted list = [(0, 'R'), (1, 'a'), (2, 'j'), (3, 'k'), (4, 'u'), (5, 'm'), (6, 'a'), (7, 'r')]
converted tuple = ((0, 'R'), (1, 'a'), (2, 'j'), (3, 'k'), (4, 'u'), (5, 'm'), (6, 'a'), (7, 'r'))
converted dictionary = {0: 'R', 1: 'a', 2: 'j', 3: 'k', 4: 'u', 5: 'm', 6: 'a', 7: 'r'}
converted set = {(4, 'u'), (7, 'r'), (5, 'm'), (6, 'a'), (1, 'a'), (2, 'j'), (0, 'R'), (3, 'k')}

In the above code we have seen how to convert an enumerate-object into different datatype .

How to get the previous string from an enumerate object ?

If we create an enumerate-object from a string , we can easily get back the string from that enumerate-object

myString="Rajkumar"
myEnumerate=enumerate(myString)

# declaring an empty string
convertedString=""

# iterating the emumerate-object and 
# adding the characters to the empty string
for i in myEnumerate:
    convertedString += i[1]
print("converted string =",convertedString) 
converted string = Rajkumar

Thank you for reading this Article . If You enjoy it Please Share the article . If you want to say something Please Comment .

Leave a Reply