w3resource

Python Itertools: Exercises, Practice, Solution

Python Itertools [44 exercises with solution]

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

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.
Click me to see the sample solution

2. Write a Python program that generates the running product of elements in an iterable.
Click me to see the sample solution

3. Write a Python program to generate the maximum and minimum values of the elements of an iterable.
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.
Click me to see the sample solution

5. Write a Python program to generate an infinite cycle of elements from an iterable.
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.
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, it returns every element.
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.
Click me to see the sample solution

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

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

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

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

13. Write a Python program that will select a specified number of colours from three different colours, and then generate all the combinations with repetitions.
Click me to see the sample solution

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

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

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

17. Write a Python program to read a given string character by character and compress repeated characters by storing the length of those character(s).
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.
Click me to see the sample solution

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

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

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

22. Write a Python program to create a 24-hour time format (HH:MM) using the 4 given digits. Display the latest time and do not use any digit more than once.
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 an itertools component to solve the problem.
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 the same. Use the itertools module to solve the problem.
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 the itertools module to solve the problem.
Click me to see the sample solution

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

27. Create a Python program that chooses a specified number of colors from three different colors and generates unique combinations.
Click me to see the sample solution

28. Write a Python program to find the maximum, minimum aggregation pair in a given list of integers.
Click me to see the sample solution

29. Write a Python program to interleave multiple lists of the same length. Use the itertools module.
Click me to see the sample solution

30. Write a Python program to create non-repeated combinations of the Cartesian product of a given list of four numbers.
Click me to see the sample solution

31. Write a Python program to count the frequency of consecutive duplicate elements in a given list of numbers. Use the itertools module.
Click me to see the sample solution

32. Write a Python program to count the frequency of the elements of a given unordered list.
Click me to see the sample solution

33. Write a Python program to find pairs of maximum and minimum products from a given list. Use the itertools module.
Click me to see the sample solution

34. Write a Python program to compute the sum of digits of each number in a given list of positive integers.
Click me to see the sample solution

35. Write a Python program to get all possible combinations of the elements of a given list using the itertools module.
Click me to see the sample solution

36. Write a Python program to add two given lists of different lengths, starting from the right, using the itertools module.
Click me to see the sample solution

37. Write a Python program to add two given lists of different lengths, starting from the left, using the itertools module.
Click me to see the sample solution

38. Write a Python program to interleave a number of lists of different lengths using the itertools module.
Click me to see the sample solution

39. Write a Python program to get the index of the first element that is greater than a specified element using the itertools module.
Click me to see the sample solution

40. Write a Python program to split a given list into specified sized chunks using the itertools module.
Click me to see the sample solution

41. Write a Python program to find all lower and upper mixed case combinations of a given string.
Click me to see the sample solution

42. Write a Python program to create groups of similar items from a given list.
Click me to see the sample solution

43. Write a Python program to find the maximum difference between pairs in a given list.
Click me to see the sample solution

44. Write a Python program to extract a non-zero block from a given integer list.
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



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://198.211.115.131/python-exercises/itertools/index.php