The problem focused in this article is quite specific and may be less useful in different domains. But the way this is going to solve may open doors to solve potentially like problems, hence making it worth a read. This article solves the problem of testing if a list contains series of odd or even elements. Let’s discuss certain ways in which this problem can be solved.
Method #1 : Using sum() + list comprehension + zip() + any()
This problem can be solved using the combination of above functions. This method solves problem in 2 steps. In 1st step, we compute all the possible pairs of N using list comprehension and zip function and in the second step we use sum and any function to test for N divisible result, if we find any one of it, we return positive.
# Python3 code to demonstrate # N consecutive Odd or Even Numbers # using sum() + zip() + any() + list comprehension # initializing list test_list = [1, 5, 6, 4, 8] # printing original list print("The original list : " + str(test_list)) # initializing N N = 3 # using sum() + zip() + any() + list comprehension # N consecutive Odd or Even Numbers temp = ( test_list[i : i + N] for i in range(len(test_list) - N + 1) ) res = any( sum(ele % 2 for ele in temps) % N == 0 for temps in temp ) # print result print("Does list contain the desired consecution : " + str(res)) |
The original list : [1, 5, 6, 4, 8] Does list contain the desired consecution : True
Method #2 : Using groupby() + any()
The whole logic of doing the 1st step in the above method can be managed using the groupby function in which we perform the grouping and any function can be used later for checking the consecution.
# Python3 code to demonstrate # N consecutive Odd or Even Numbers # using groupby() + any() from itertools import groupby # initializing list test_list = [1, 5, 6, 4, 8] # printing original list print("The original list : " + str(test_list)) # initializing N N = 3 # using groupby() + any() # N consecutive Odd or Even Numbers res = any(len(list(sub)) == N for idx, sub in groupby([sub % 2 for sub in test_list])) # print result print("Does list contain the desired consecution : " + str(res)) |
The original list : [1, 5, 6, 4, 8] Does list contain the desired consecution : True
Recommended Posts:
- Python program to check if the list contains three consecutive common numbers in Python
- Python | Check if list contains consecutive numbers
- Python - K consecutive Maximum
- Python | Mean of consecutive Sublist
- Python | Retain K consecutive elements
- Python - K length consecutive characters
- Python - Double each consecutive duplicate
- Python | Consecutive Pair Minimums
- Python | Remove Consecutive tuple according to key
- Python | Consecutive String Comparison
- Python - Consecutive Missing elements Sum
- Python | Consecutive chunks Product
- Python - Consecutive Repetition of Characters
- Python | Consecutive characters frequency
- Python | Consecutive Subsets Minimum
- Python - Extend consecutive tuples
- Python - Consecutive Tuple difference
- Python - Consecutive Row summation in Matrix
- Python - First K consecutive digits in String
- Python | Minimum Sum of Consecutive Characters
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

