# Python | N consecutive Odd or Even Numbers

• Last Updated : 29 Apr, 2019

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))`

Output :

```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))`

Output :

```The original list : [1, 5, 6, 4, 8]
Does list contain the desired consecution : True
```

My Personal Notes arrow_drop_up