Home  >  Article  >  Backend Development  >  What is the python fromkeys function? What is the role of fromkeys function?

What is the python fromkeys function? What is the role of fromkeys function?

乌拉乌拉~
乌拉乌拉~Original
2018-08-18 17:55:067456browse

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

##seq - - List of dictionary keys.

value -- Optional parameter, set the value of the key sequence (seq).

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)

The output result of the above example is:

新字典为 : {'Google': None, 'Taobao': None, 'Runoob': None}
新字典为 : {'Google': 10, 'Taobao': 10, 'Runoob': 10}

The above is all the content of this article, the built-in fromkeys function of the dictionary in python. I hope what I said and the examples I gave can be helpful to you.

For more related knowledge, please visit the

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn