w3resource

Python Itertools: Exercises, Practice, Solution

Python Itertools [26 exercises with solution]

[An editor is available at the bottom of the page to write and execute the scripts.]

Python itertools module implements a number of iterator building blocks inspired by constructs from APL, Haskell, and SML. Each has been recast in a form suitable for Python. The module standardizes a core set of fast, memory efficient tools that are useful by themselves or in combination.

1. Write a Python program to create an iterator from several iterables in a sequence and display the type and elements of the new iterator. Go to the editor
Click me to see the sample solution

2. Write a Python program to generate the running product of the elements of an given iterable. Go to the editor
Click me to see the sample solution

3. Write a Python program to generate the running maximum, minimum value of the elements of an iterable. Go to the editor
Click me to see the sample solution

4. Write a Python program to construct an infinite iterator that returns evenly spaced values starting with a specified number and step. Go to the editor
Click me to see the sample solution

5. Write a Python program to generate an infinite cycle of elements from an iterable. Go to the editor
Click me to see the sample solution

6. Write a Python program to make an iterator that drops elements from the iterable as soon as an element is a positive number. Go to the editor
Click me to see the sample solution

7. Write a Python program to make an iterator that drops elements from the iterable as long as the elements are negative; afterwards, returns every element. Go to the editor
Click me to see the sample solution

8. Write a Python program to create an iterator that returns consecutive keys and groups from an iterable. Go to the editor
Click me to see the sample solution

9. Write a Python program to split an iterable and generate iterables specified number of times. Go to the editor
Click me to see the sample solution

10. Write a Python program to create an iterator to get specified number of permutations of elements. Go to the editor
Click me to see the sample solution

11. Write a Python program to generate combinations of a given length of given iterable. Go to the editor
Click me to see the sample solution

12. Write a Python program to create Cartesian product of two or more given lists using itertools. Go to the editor
Click me to see the sample solution

13. Write a Python program to chose specified number of colours from three different colours and generate all the combinations with repetitions. Go to the editor
Click me to see the sample solution

14. Write a Python program generate permutations of specified elements, drawn from specified values. Go to the editor
Click me to see the sample solution

15. Write a Python program to generate all possible permutations of n different objects. Go to the editor
Click me to see the sample solution

16. Write a Python program find the sorted sequence from a set of permutations of a given input. Go to the editor
Click me to see the sample solution

17. Write a Python program to read a given string character by character and compress repeated character by storing the length of those character(s). Go to the editor
Click me to see the sample solution

18. Write a Python program to generate permutations of n items in which successive permutations differ from each other by the swapping of any two items. Go to the editor
Click me to see the sample solution

19. Write a Python program which iterates the integers from 1 to a given number and print "Fizz" for multiples of three, print "Buzz" for multiples of five, print "FizzBuzz" for multiples of both three and five using itertools module. Go to the editor
Click me to see the sample solution

20. Write a Python program to find the factorial of a number using itertools module. Go to the editor
Click me to see the sample solution

21. Write a Python program to find the years where 25th of December be a Sunday between 2000 and 2150. Go to the editor
Click me to see the sample solution

22. Write a Python program to create a 24-hour time format (HH:MM ) using 4 given digits. Display the latest time and do not use any digit more than once. Go to the editor
Click me to see the sample solution

23. Write a Python program to find the shortest distance from a specified character in a given string. Return the shortest distances through a list and use itertools module to solve the problem. Go to the editor
Click me to see the sample solution

24. Write a Python program to find the maximum length of a substring in a given string where all the characters of the substring are same. Use itertools module to solve the problem. Go to the editor
Click me to see the sample solution

25. Write a Python program to find the first two elements of a given list whose sum is equal to a given value. Use itertools module to solve the problem. Go to the editor
Click me to see the sample solution

26. Write a Python program to find the nth Hamming number. User itertools module. Go to the editor
Click me to see the sample solution

Python Code Editor:


More to Come !

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.

Test your Python skills with w3resource's quiz



Python: Tips of the Day

Python: While-else construct

i = 5

while i > 1:
    print("Whil-ing away!")
    i -= 1
    if i == 3:
        break
else:
    print("Finished up!")