April 19, 2024, 11:17 p.m. | Main

DEV Community dev.to

Thefromkeys() method of dicttype is used to create a new dictionary with keys from a given iterable and values pre-filled with a specified default value.


The syntax is as shown below:




dict.setdefault(iterable, value = None)


The items in the created dictionary has the elements from the iterableas keys and the specified valueas the default value. If valueis not given, Nonewill be used.




keys = [1, 2, 3, 4, 5]

d = dict.fromkeys(keys)
print(d)


If the default value is given, the …

create dictionary iterable keys python syntax value values

AI Research Scientist

@ Vara | Berlin, Germany and Remote

Data Architect

@ University of Texas at Austin | Austin, TX

Data ETL Engineer

@ University of Texas at Austin | Austin, TX

Lead GNSS Data Scientist

@ Lurra Systems | Melbourne

Senior Machine Learning Engineer (MLOps)

@ Promaton | Remote, Europe

AI Engineering Manager

@ M47 Labs | Barcelona, Catalunya [Cataluña], Spain