This thought came up in the discussion thread for PEP 798 and I thought it might be worth some more discussion.
We can currently add elements from a whole bunch of iterables to a set by calling set.update with multiple arguments passed in:
>>> L = [[1,2,3], [], [4,5]]
>>> x = set()
>>> x.update(*L)
>>> x
{1, 2, 3, 4, 5}
However, list.extend and dict.update can’t be called in a similar way. For those, we would need a loop and multiple method calls to get the same result:
>>> L = [[1,2,3], [], [4,5]]
>>> x = []
>>> for list_ in L:
... x.extend(list_)
...
>>> x
[1, 2, 3, 4, 5]
>>> L = [{1: 2}, {}, {3: 4, 1: 10}, [(9, 10)]]
>>> x = {}
>>> for dict_ in L:
... x.update(dict_)
...
>>> x
{1: 10, 3: 4, 9: 10}
I think we should consider making list.extend and dict.update variadic as well, making them behave more like set.update and allowing code like the following:
>>> L = [[1,2,3], [], [4,5]]
>>> x = []
>>> x.extend(*L)
>>> x
[1, 2, 3, 4, 5]
>>> L = [{1: 2}, {}, {3: 4, 1: 10}, [(9, 10)]]
>>> x = {}
>>> x.update(*L)
>>> x
{1: 10, 3: 4, 9: 10}
I put together a quick proof-of-concept implementation of this idea, and an Emscripten-based demo in case anyone wants to play with this idea without needing to compile it yourself.
I’m curious what other people think.