Python Intermediate: What are the Looping Techniques in Python

Home /

Table of Contents

Looping Techniques in Python

Python has a number of built-in methods that allow different looping strategies in a number of sequential containers. These techniques are very helpful in programming competitions and other projects that call for a certain approach using loops that preserve the general structure of the code. As there is no need to declare the additional variables that we declare in the conventional method to loops, a great deal of time and memory space is saved.

Using enumerate()

To iterate over the containers, use the enumerate() function, which prints the index number and the value for each one along with the container it is located in.

Example:

for key, value in enumerate(['Saturday', 'Sunday', 'Monday', 'Tuesday', 'Wednesday','Thursday', 'Friday']):
   print(key, value)

Output:

0 Saturday
1 Sunday
2 Monday
3 Tuesday
4 Wednesday
5 Thursday
6 Friday

Using zip()

When two comparable containers (such as a list of lists or a dict of dicts) are combined, zip() is used to output the items consecutively. Only until the smaller container is empty does the cycle continue.

Example:

 
ask = ['color','game', 'programming language']
ans = ['green', 'cricket', 'python']
 
 
for P, Q in zip(ask, ans):
   print('My favorite {0}:- {1}.'.format(P, Q))
 

Output:

My favorite colour:- green.
My favorite game:- cricket.
My favorite programming language:- python.

Using items()

The items() function in the Python dictionary is used to return a list of all dictionary keys and values. There are no arguments required for this technique. What is returned is a view object that displays a list of all the (key, value) tuple pairs in the supplied dictionary.

Example:

dict = {"name:": "karim", "learn:": "python"}
 
print("The key-value pair using items is : ")
for p, q in dict.items():
 print(p, q)
 

Output:

The key-value pair using items is :
name: karim
learn: python

Using sorted()

The container is printed in sorted order using the sorted() function. It just prints the container in sorted order for one time rather than sorting the container. Duplicate occurrences can be eliminated by combining the usage of a set().

Example:

nums = [12, 13, 15, 16, 12, 11, 13]
 
print("Sorted List: ")
for i in sorted(nums):
 print(i, end=" ")
 
print()
print("Sorted order (without duplicates) : ")
for i in sorted(set(nums)):
 print(i, end=" ")
 

Output:

Sorted List:
11 12 12 13 13 15 16
Sorted order (without duplicates) :
11 12 13 15 16

Using reversed()

The values of the container are printed in reverse order using the reversed() function. No alterations to the initial list are reflected in it.

Example:

nums = [11, 12, 13, 14, 15, 16, 17]
 
 
print("Reversed order: ")
for i in reversed(nums):
 print(i, end=" ")
 

Output:

Reversed order:
17 16 15 14 13 12 11

Share The Tutorial With Your Friends
Twiter
Facebook
LinkedIn
Email
WhatsApp
Skype
Reddit
Other Recommended Article