I have a nested for loop which is not running after the first iteration
N, M = map(int, input().split())
numbers = map(int, input().split())
dic = {}
for m in range(N):
dic.setdefault(m,[])
for n in numbers:
if n % N == m:
dic[m].append(n)
print(dic)
The above code is generating the following result {0: [3, 42], 1: [], 2: []}
for the sample data down bellow:
3 5
1 3 8 10 42
However I would like to get {0: [3, 42], 1: [1, 10], 2: [8]}
What am I doing wrong?
The problem is that map
returns an iterator, and you are consuming the iterator completely in the first outer loop. You need:
numbers = [int(k) for k in input().split()]
instead of using map
.