If you look at the docstring
for extend
, you will see that it can be extended by any iterable:
""" L.extend(iterable) -> None -- extend list by appending elements from the iterable """
This means you are not only limited to list
s or tuple
s, but also string
s, set
s, and other iterables:
for iterable in ('ab', set([1, 2, 3]), {1:2, 3:4}, (0, 1)):
l = [1]
l.extend(iterable)
print(l)
This prints:
[1, 'a', 'b']
[1, 1, 2, 3]
[1, 1, 3] # Note dictionaries iterate over keys by default
[1, 0, 1]
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…