I am trying to understand the code below:
from functools import reduce
reduce(lambda d, i: (i[0] < 7 and d.__setitem__(*i[::-1]), d)[-1], [{}, *{1:2, 3:4, 5:6, 7:8}.items()])
The part that I don't understand is the *.
[{}, *{1:2, 3:4, 5:6, 7:8}.items()]
returns:
[{}, (1, 2), (3, 4), (5, 6), (7, 8)]
Now if i run in isolation
*{1:2, 3:4, 5:6, 7:8}.items()
it returns an error : "File "cell_name", line 4 SyntaxError: can't use starred expression here". However I am able to run the code without * just fine as a one-line code and returns:
dict_items([(1, 2), (3, 4), (5, 6), (7, 8)])
I have googled it and in https://medium.com/understand-the-python/understanding-the-asterisk-of-python-8b9daaa4a558 it seems related to unpacking containers, but this one is used in arrays.
However in the full lambda, the * seems necessary and throws an error "'dict_items' object does not support indexing" without the *.
Anyone understand this syntax jargon? Thanks for any help.