﻿ Python: Extract non_zero block from a given integers of list - w3resource

# Python: Extract non_zero block from a given integers of list

## Python Itertools: Exercise-44 with Solution

Write a Python program to extract a non-zero block from a given integer list.

Sample Solution:

Python Code:

``````import itertools as it
def test(lst):
result = [ list(x[1]) for x in it.groupby(lst, lambda x: x == 0) if not x[0] ]
return result
nums = [3,4,6,2,0,0,0,0,0,0,6,7,6,9,10,0,0,0,0,0,7,4,4,0,0,0,0,0,0,5,3,2,9,7,1]
print("\nOriginal list:")
print(nums)
print("\nExtract non_zero block from a given integers of list:")
print(test(nums))
```
```

Sample Output:

```Original list:
[3, 4, 6, 2, 0, 0, 0, 0, 0, 0, 6, 7, 6, 9, 10, 0, 0, 0, 0, 0, 7, 4, 4, 0, 0, 0, 0, 0, 0, 5, 3, 2, 9, 7, 1]

Extract non_zero block from a given integers of list:
[[3, 4, 6, 2], [6, 7, 6, 9, 10], [7, 4, 4], [5, 3, 2, 9, 7, 1]]
```

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

﻿

## Python: Tips of the Day

Inverts a dictionary with non-unique hashable values:

Example:

```def tips_collect_dictionary(obj):
inv_obj = {}
for key, value in obj.items():
inv_obj.setdefault(value, list()).append(key)
return inv_obj
ages = {
"Owen": 25,
"Jhon": 25,
"Pepe": 15,
}
print(tips_collect_dictionary(ages))
```

Output:

```{25: ['Owen', 'Jhon'], 15: ['Pepe']}
```

We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook