[Tutor] the "**" operator?
Piotr Kamiński
piotr-kam at o2.pl
Fri Dec 10 20:41:02 CET 2010
Dnia 10-12-2010 o 20:14:30 Alex Hall <mehgcap at gmail.com> napisał(a):
> Hi all,
> I was googling a way to do something like
> mydict=mydict.extend(additionaldict)
>
> and someone on a forum recommends this:
> mydict=dict(mydict, **additionaldict)
> What is the ** doing here?
As far as I know the ** indicates that the argument passed (here named:
additionaldict) is a dictionary. One * would mean that it is a tuple.
I'm not sure but it seams to me that the usual way to *update* a
dictionary with the contents of an other dictionary is to use the .update
dictionary method.
Examples:
>>> animals= {'cat': 'Bobo', 'dog': 'Rex'}
>>> animals2= {'cow': 'some name that I can\'t think of right now',
>>> 'bird': 'Falco'}
>>> animals.update(animals2)
>>> print animals
{'bird': 'Falco', 'dog': 'Rex', 'cow': "some name that I can't think of
right now", 'cat': 'Bobo'}
>>> print animals2
{'bird': 'Falco', 'cow': "some name that I can't think of right now"}
>>>
Hope my answers are right and it helps,
Piotr
More information about the Tutor
mailing list