“Python Merge List of Dict In Single Dict” Réponses codées

Créer un dictionnaire Python à partir de deux listes

>>> keys = ['a', 'b', 'c']
>>> values = [1, 2, 3]
>>> dictionary = dict(zip(keys, values))
>>> print(dictionary)
{'a': 1, 'b': 2, 'c': 3}
Defeated Dotterel

Python fusionne les dictionnaires

dict1 = {'color': 'blue', 'shape': 'square'}
dict2 = {'color': 'red', 'edges': 4}

dict1.update(dict2) #if a key exists in both, it takes the value of the second dict
# dict1 = {'color': 'red', 'shape': 'square', 'edges': 4}
# dict2 is left unchanged
MitroGr

Python Merge List of Dict In Single Dict

l = [{'a': 0}, {'b': 1}, {'c': 2}, {'d': 3}, {'e': 4, 'a': 4}]
d = {k: v for x in l for k, v in x.items()}
print(d)
# {'a': 4, 'b': 1, 'c': 2, 'd': 3, 'e': 4}
gdfelt

Réponses similaires à “Python Merge List of Dict In Single Dict”

Questions similaires à “Python Merge List of Dict In Single Dict”

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code