w3resource

Python Itertools: Exercises, Practice, Solution


This resource offers a total of 220 Python Itertools problems for practice. It includes 44 main exercises, each accompanied by solutions, detailed explanations, and four related problems.

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.

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


1. Iterator from Iterables

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. Running Product Iterator

Write a Python program that generates the running product of elements in an iterable.

Click me to see the sample solution


3. Max & Min from Iterable

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. Infinite Iterator with Step

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. Infinite Cycle of Elements

Write a Python program to generate an infinite cycle of elements from an iterable.

Click me to see the sample solution


6. Drop Until Positive

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. Drop While Negative

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. Consecutive Keys Group

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. Split Iterable into Chunks

Write a Python program to split an iterable and generate iterables a specified number of times.

Click me to see the sample solution


10. Permutations Iterator

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. Combinations of Given Length

Write a Python program to generate combinations of a given length of a given iterable.

Click me to see the sample solution


12. Cartesian Product of Lists

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. Color Combinations with Repetition

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. Specified Element Permutations

Write a Python program to generate permutations of specified elements drawn from specified values.

Click me to see the sample solution


15. All Permutations of n Objects

Write a Python program to generate all possible permutations of n different objects.

Click me to see the sample solution


16. Sorted Permutation Sequence

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. String Compression

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. Swapping Successive Permutations

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. FizzBuzz with Itertools

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. Factorial using Itertools

Write a Python program to find the factorial of a number using the itertools module.

Click me to see the sample solution


21. Christmas Sunday Finder

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. 24-Hour Time from Digits

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. Shortest Distance for Character

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. Longest Uniform Substring

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. First Two Sum Elements

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. Nth Hamming Number

Write a Python program to find the nth Hamming number. Use the itertools module.

Click me to see the sample solution


27. Unique Color Combinations

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. Aggregation Pair (Max,Min)

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. Interleave Multiple Lists

Write a Python program to interleave multiple lists of the same length. Use the itertools module.

Click me to see the sample solution


30. Non-repeated Cartesian Combinations

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. Count Consecutive Duplicates

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. Frequency of Unordered List

Write a Python program to count the frequency of the elements of a given unordered list.

Click me to see the sample solution


33. Max-Min Product Pairs

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. Sum of Digits for Each Number

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. All Combinations of List Elements

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. Add Lists from Right

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. Add Lists from Left

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. Interleave Different Length Lists

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. Index of First Greater Element

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. Split List into Chunks

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. Mixed Case Combinations

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. Group Similar Items

Write a Python program to create groups of similar items from a given list.

Click me to see the sample solution


43. Max Difference Between Pairs

Write a Python program to find the maximum difference between pairs in a given list.

Click me to see the sample solution


44. Extract Non-zero Block

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



Follow us on Facebook and Twitter for latest update.