• Python: 3.8.11
  • OS: Ubuntu Kylin 20.04
  • Conda: 4.10.1
  • Jupyter lab: 3.1.4

code

Formkeys returns a new dictionary

Create an empty dictionary
dict_empty = {}
my_dict = dict_empty.fromkeys((1.2.3))
dict_empty,my_dict

({}, {1: None.2: None.3: None})
Copy the code
# formkeys has the same value for each key
my_dict = dict_empty.fromkeys((1.2.3), 'number')
print(my_dict)

{1: 'number'.2: 'number'.3: 'number'}
Copy the code

Knowledge extension

help(dict.fromkeys)


Help on built-in function fromkeys:

fromkeys(iterable, value=None, /) method of builtins.type instance
    Create a new dictionary with keys from可迭代and values set to value.
Copy the code

Learning to recommend

  • Python documentation – English
  • Python documentation – Chinese
  • Python code PEP
  • Google version of the Python specification
  • Python source code
  • Python PEP
  • Optimal kirin
  • The nuggets platform
  • Gitee platform


Python is open source, cross-platform, interpretive, interactive, and worth learning. Python’s design philosophy: elegant, unambiguous, simple. Advocate one way, preferably only one way to do one thing. Code should be written in accordance with specifications to facilitate communication and understanding. Every language has its own unique ideas. Beginners need to change their thinking, practice and accumulate.