In this article, we will learn about the python fromkeys function in the python dictionary. What does the fromkeys function mean? What effect does this function have? Get the answer in the following article.
Description
The Python dictionary fromkeys() function is used to create a new dictionary, using the elements in the sequence seq as the keys of the dictionary, and value is the initial value corresponding to all keys in the dictionary. value.
Syntax
romkeys() method syntax:
dict.fromkeys(seq[, value])
Parameters
Return value
This method returns a new dictionary.Example
The following example shows how to use the fromkeys() function:# !/usr/bin/python # -*- coding: UTF-8 -*- seq = ('Google', 'Runoob', 'Taobao') dict = dict.fromkeys(seq) print "新字典为 : %s" % str(dict) dict = dict.fromkeys(seq, 10) print "新字典为 : %s" % str(dict)
新字典为 : {'Google': None, 'Taobao': None, 'Runoob': None} 新字典为 : {'Google': 10, 'Taobao': 10, 'Runoob': 10}
Python tutorial column on the php Chinese website.
The above is the detailed content of What is the python fromkeys function? What is the role of fromkeys function?. For more information, please follow other related articles on the PHP Chinese website!