w3resource

Python Itertools: Exercises, Practice, Solution

Python Itertools [42 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

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

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

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

30. Write a Python program to create non-repeated combinations of Cartesian product of four given list of numbers. Go to the editor
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 itertools module. Go to the editor
Click me to see the sample solution

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

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

34. Write a Python program to compute the sum of digits of each number of a given list of positive integers. Go to the editor
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 itertools module. Go to the editor
Click me to see the sample solution

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

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

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

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

40. Write a Python program to split a given list into specified sized chunks using itertools module. Go to the editor
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. Go to the editor
Click me to see the sample solution

42. Write a Python program to create group of similar items of a given list. 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

Converts a number to a list of digits

Example:

def tips_digit(n):
  return list(map(int, str(n)))

print(tips_digit(2468))

Output:

[2, 4, 6, 8]