English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Tutoriel de base Python

Contrôle de flux Python

Fonctions en Python

Types de données en Python

Opérations de fichiers Python

Objets et classes Python

Dates et heures Python

Connaissances avancées Python

Manuel de référence Python

Utilisation et exemple de fromkeys() dans le dictionnaire Python

Python dictionary methods

La méthode fromkeys() crée un nouveau dictionnaire basé sur une séquence d'éléments donnés, avec des valeurs fournies par l'utilisateur.

La syntaxe de la méthode fromkeys() est :

dictionary.fromkeys(sequence[, value])

Paramètres de fromkeys()

La méthode fromkeys() prend deux paramètres :

  • sequence -Séquence d'éléments utilisée comme clés du nouveau dictionnaire

  • value (optionnel) -Valeur définie pour chaque élément du dictionnaire

Retour de fromkeys()

La méthode fromkeys() retourne un nouveau dictionnaire qui a une séquence d'éléments donnés en tant que clés du dictionnaire.

If the value parameter is set, each element of the newly created dictionary will be set to the provided value.

Example1: Create a dictionary based on the sequence of keys

# Vowel keys
keys = {'a', 'e', 'i', 'o', 'u'}
vowels = dict.fromkeys(keys)
print(vowels)

When running the program, the output is:

{'a': None, 'u': None, 'o': None, 'e': None, 'i': None}

Example2: Create a dictionary based on the sequence of keys with values

# Vowel keys
keys = {'a', 'e', 'i', 'o', 'u'}
value = 'vowel'
vowels = dict.fromkeys(keys, value)
print(vowels)

When running the program, the output is:

{'a': 'vowel', 'u': 'vowel', 'o': 'vowel', 'e': 'vowel', 'i': 'vowel'}

Example3: Create a dictionary from a list of mutable objects

# Vowel keys
keys = {'a', 'e', 'i', 'o', 'u'}
value = [1]
vowels = dict.fromkeys(keys, value)
print(vowels)
# Updated value
value.append(2)
print(vowels)

When running the program, the output is:

{'a': [1], 'u': [1], 'o': [1], 'e': [1], 'i': [1}]
{'a': [1, 2], 'u': [1, 2], 'o': [1, 2], 'e': [1, 2], 'i': [1, 2}]

If the provided value is a mutable object (whose value can be changed), such aslist,dictionaryIf a mutable object, such as a list, is modified, each element in the sequence will also be updated.

This is because, for each element, a reference to the same object (pointing to the same object in memory) is assigned.

To avoid this problem, we use dictionary comprehension.

# Vowel keys
keys = {'a', 'e', 'i', 'o', 'u'}
value = [1]
vowels = { key: list(value) for key in keys }
# You can also use { key: value[:] for key in keys }
print(vowels)
# Updated value
value.append(2)
print(vowels)

When running the program, the output is:

{'a': [1], 'u': [1], 'o': [1], 'e': [1], 'i': [1}]
{'a': [1], 'u': [1], 'o': [1], 'e': [1], 'i': [1}]

Here, for each key in keys, a new list is created from value and assigned to it.

In essence, value is not assigned to the element, but a new list is created from it, and then it is assigned to each element in the dictionary.

Python dictionary methods