Python Inbuilt Looping Functions | FACE Prep

Some of the Python inbuilt looping functions are enumerate(), zip(), iteritems(), items(), etc. These functions help in looping through an iterable without the use of for loop or while loop. Also, they are used to print the elements of the iterable without changing the original elements of the iterable. 

Advantages of using these inbuilt looping functions over for and while loop

There are several advantages of using these inbuilt looping functions over traditional for and while loop. Some of them are:

  • They are easy to use as they reduce the coding effort
  • They make the code more concise than for and while loops

Let us now see some of the inbuilt functions used for looping in detail.

Inbuilt Looping Functions

a) enumerate() 

This function adds a counter to an iterable and returns the iterable in the form of an enumerate object. As a result, when we print that object, the elements in the iterable along with their respective indices get printed. Have a look at the below example code to understand how a list gets converted to an enumerate object.

list1 = ["FACEPrep", 40, 50.67]
obj = enumerate(list1) #converting an iterable into an enumerate object
print(obj)
Output:
<enumerate object at 0x7f8a96659b88>

print(obj) will print only the address of that object. To print elements, we need to use either a list() or a tuple() method. The elements in the enumerate object can be printed as a list of tuples or as a tuple of tuples using the list() and tuple() methods respectively as shown below.

#printing elements of an enumerate object as a list of tuples
list1 = ["FACEPrep", 40, 50.67]
obj = enumerate(list1)  #converting an iterable into enumerate object
print(list(obj))

#printing elements of an enumerate object as a tuple of tuples
list1 = ["FACEPrep", 40, 50.67]
obj = enumerate(list1)  #converting an iterable into enumerate object
print(tuple(obj))
Output:
[(0, 'FACEPrep'), (1, 40), (2, 50.67)]
((0, 'FACEPrep'), (1, 40), (2, 50.67))

b) zip() 

zip() function is used to combine elements of multiple iterables with same index values. This function takes multiple iterables as arguments. The elements in the zip object can be printed as a list of tuples or as a tuple of tuples using the list() and tuple() methods respectively as shown below.
#printing elements of a zip object as a list of tuples
list1 = ["I", "learning", "in"]
list2 = ["love", "Python", "FACEPrep"]
obj = zip(list1, list2)  #converting an iterable into zip object
print(list(obj))

#printing elements of a zip object as a tuple of tuples
list1 = ["I", "learning", "in"]
list2 = ["love", "Python", "FACEPrep"]
obj = zip(list1, list2)  #converting an iterable into zip object
print(tuple(obj))
Output:
[('I', 'love'), ('learning', 'Python'), ('in', 'FACEPrep')]
(('I', 'love'), ('learning', 'Python'), ('in', 'FACEPrep'))

c) iteritems() 

iteritems() function is used to traverse through a dictionary and print the key-value pairs. This function works only in Python 2.x versions. The key-values are printed as a list of tuple pairs or as a tuple of tuple pairs using list() and tuple() methods respectively as shown below.

#printing elements of a iteritems object as a list of tuples
dict1 = {"Name": "Ball", "Color": "Red", "Shape": "Round"}
obj = dict1.iteritems()  #converting an iterable into iteritems object
print(list(obj))

#printing elements of a iteritems object as a tuple of tuples
dict1 = {"Name": "Ball", "Color": "Red", "Shape": "Round"}
obj = dict1.iteritems()  #converting an iterable into iteritems object
print(tuple(obj))
Output:
[('Color', 'Red'), ('Shape', 'Round'), ('Name', 'Ball')]
(('Color', 'Red'), ('Shape', 'Round'), ('Name', 'Ball'))

d) items() 

items() function is also used to traverse through a dictionary and print the key-value pair. This function works the same as that of iteritems() function. But, items() function can be implemented in Python 3.x versions. The disadvantage of using this function is, it consumes more time and occupies double the memory space when applied to a large dictionary. Have a look at the below example, where the key-value pairs of a dictionary are printed as a list of tuple pairs.
dict1 = {"Name": "Ball", "Color": "Red", "Shape": "Round"}
obj = dict1.items() #converting an iterable into items object
print(list(obj))
Output:
[('Name', 'Ball'), ('Color', 'Red'), ('Shape', 'Round')]

e) sorted() 

sorted() function is used to print the elements of an iterable in sorted order. This function sorts the elements but does not store the result in the original iterable. In the below-given example code, the elements of the iterable are sorted using sorted() function and gets printed in the form of a list using list() method. Later when we print the iterable, the elements still get printed in the original order.
fruits = ["Banana", "Mango", "Apple"]
obj = sorted(fruits) #sorting the elements of an iterable
print(list(obj))
print(fruits)
Output:
['Apple', 'Banana', 'Mango']
['Banana', 'Mango', 'Apple']

f) reversed() 

reversed() function is used to print the elements of an iterable in reverse order, i.e. the element at the last index will be printed first and so on. This function reverses the elements but does not store the result in the original iterable as shown below.

list1 = [5, 3, 8, 9]
obj = reversed(list1) #reversing the iterable
print(list(obj))
print(list1)
Output:
[9, 8, 3, 5]
[5, 3, 8, 9]

Summary

In Python, inbuilt looping functions are used to loop through an iterable without the use of a for loop or a while loop. Since these functions do not have any syntax and conditions, they are easy to implement. Also, there is no need to declare any variables beforehand to access elements of the iterable using these inbuilt functions.