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

乌拉乌拉~
Release: 2018-08-18 17:55:06
Original
7472 people have browsed it

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])
Copy after login

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)
Copy after login

The output result of the above example is:

新字典为 : {'Google': None, 'Taobao': None, 'Runoob': None}
新字典为 : {'Google': 10, 'Taobao': 10, 'Runoob': 10}
Copy after login
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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template