Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there putAll like method for dict in python?

Tags:

python

Is there a method like putAll() for dict in python? I did not find a method in the document that seems to do this. Sure I can iterate over the key set and put every thing in the new dict, but I think there might be a better way.

Also (this might be another question), is 'Python Library Reference' considered the API document for python? I'm more used to javadoc like API document that has a list of classes and methods. With 'Python Library Reference', I always wonder if all methods of a class are listed and there's no link that I can jump around to see the definition of parameters and return type.

like image 242
DenMark Avatar asked Mar 27 '12 02:03

DenMark


People also ask

Should I use dict () or {}?

With CPython 2.7, using dict() to create dictionaries takes up to 6 times longer and involves more memory allocation operations than the literal syntax. Use {} to create dictionaries, especially if you are pre-populating them, unless the literal syntax does not work for your case.

Can you use map on dictionary Python?

Using map() function to transform Dictionaries in Python map() function iterated over all the items in dictionary and then applied passed lambda function on each item. Which in turn updated the value of each item and returns a copy of original dictionary with updated contents.

How does putAll work?

putAll() is an inbuilt method of HashMap class that is used for the copy operation. The method copies all of the elements i.e., the mappings, from one map into another. Parameters: The method takes one parameter exist_hash_map that refers to the existing map we want to copy from.

What is dict str any?

For example, dict[int, str] is a dictionary from integers to strings and dict[Any, Any] is a dictionary of dynamically typed (arbitrary) values and keys. list is another generic class.


1 Answers

If you are talking about Java's putAll for maps, dict.update would be the equivalent in Python:

>>> d1 = { 1: 2, 3: 4 }
>>> d2 = { 5: 6, 3: 1 }
>>> d1.update(d2)
>>> d1
{1: 2, 3: 1, 5: 6}

Regarding the documentation, there is an overview which links all the specific module documentation. In your specific instance, you'd then click on Mapping Types — dict and browser through the available operations.

like image 92
Niklas B. Avatar answered Nov 03 '22 23:11

Niklas B.