0
n, m = map(int, [1, 2])

will got n == 1, m == 2

but:

n, m, r = map(int, [1, 2]), defaultdict(list)

will raise:

ValueError: not enough values to unpack (expected 3, got 2)

this time, n is <map object at ...>, m is the defaultdict

I am very puzzled.

Dimitris Fasarakis Hilliard
  • 150,925
  • 31
  • 268
  • 253
Rancho
  • 1,988
  • 2
  • 12
  • 12

1 Answers1

0

The structure to be unpacked here is a tuple with two elements, a nested tuple eventually containing 1 and 2 as the mapping result and the defaultdict.

((1, 2), defaultdict(list))

If you need to unpack it correctly, use parentheses at the target of the assignment to specify the structure produced:

(n, m), r = map(int, [1, 2]), defaultdict(list)

with n, m and r having their correct values.

If you use:

n, m = map(int, [1, 2]), defaultdict(list)

the map result (the iterator) will never get unpacked and simply get assigned as the value for n.

Dimitris Fasarakis Hilliard
  • 150,925
  • 31
  • 268
  • 253