Python: Dictionary with multiple values per key Leave a Comment / dictionary, Python / By Varun
In this article, we will discuss how to create and manage a dictionary in which keys can have multiple values.In python, if we want a dictionary in which one key has multiple values, then we need to associate an object with each key as value. This value object should be capable of having various values inside it. We can either use a tuple or a list as a value in the dictionary to associate multiple values with a key.Let’s understand it by some examples,
Create a dictionary with a list as the value # Create a dictionary where multiple values are # associated with a key word_freq = {'is': [1, 3, 4, 8, 10], 'at': [3, 10, 15, 7, 9], 'test': [5, 3, 7, 8, 1], 'this': [2, 3, 5, 6, 11], 'why': [10, 3, 9, 8, 12]}
In this dictionary, keys are strings, and with each key string, we have a list of numbers associated as a value field. So, basically, in this dictionary, multiple values are associated with a key.












