How to create a Dictionary from Separate Lists of Keys and Values in Python
In Python, a dictionary is a collection of key-value pairs. You can create a dictionary by enclosing a comma-separated list of key-value pairs in curly braces {}. For example, the following code creates a dictionary that maps country names to their capital cities:
countries = { 'India': 'New Delhi', 'United States': 'Washington D.C.', 'United Kingdom': 'London', }
Method 1: zip function
Sometimes, you may have two separate lists of keys and values that you want to use to create a dictionary. In this case, you can use the zip function to combine the two lists into a single iterable, and then pass the result to the dict function to create the dictionary.
Here's an example of how you can use zip and dict to create a dictionary from separate lists of keys and values:
keys = ['India', 'United States', 'United Kingdom'] values = ['New Delhi', 'Washington D.C.', 'London'] countries = dict(zip(keys, values)) print(countries) # {'India': 'New Delhi', 'United States': 'Washington D.C.', 'United Kingdom': 'London'}
Method 2: dict comprehension
Alternatively, you can use a dictionary comprehension to create the dictionary. A dictionary comprehension is similar to a list comprehension, but it allows you to create a dictionary by iterating over an iterable and constructing key-value pairs.
Here's how you can use a dictionary comprehension to create a dictionary from separate lists of keys and values:
keys = ['India', 'United States', 'United Kingdom'] values = ['New Delhi', 'Washington D.C.', 'London'] countries = {key: value for key, value in zip(keys, values)} print(countries) # {'India': 'New Delhi', 'United States': 'Washington D.C.', 'United Kingdom': 'London'} You can also use the enumerate function to iterate over the lists and create the dictionary. enumerate returns a tuple containing the index and the value for each element in the list, so you can use it to create a dictionary that maps the indices of the keys and values to the corresponding keys and values. Here's an example of how you can use enumerate and a dictionary comprehension to create a dictionary from separate lists of keys and values: ```python keys = ['India', 'United States', 'United Kingdom'] values = ['New Delhi', 'Washington D.C.', 'London'] countries = {key: values[i] for i, key in enumerate(keys)} print(countries) # {'India': 'New Delhi', 'United States': 'Washington D.C.', 'United Kingdom': 'London'}
Method 3: for-loop
Finally, you can use a for loop to iterate over the lists and add the key-value pairs to an empty dictionary.
Here's an example of how you can use a for loop to create a dictionary from separate lists of keys and values:
keys = ['India', 'United States', 'United Kingdom'] values = ['New Delhi', 'Washington D.C.', 'London'] countries = {} for i in range(len(keys)): countries[keys[i]] = values[i] print(countries) # {'India': 'New Delhi', 'United States': 'Washington D.C.', 'United Kingdom': 'London'}
You can also use the for loop in combination with the zip function to simplify the code:
keys = ['India', 'United States', 'United Kingdom'] values = ['New Delhi', 'Washington D.C.', 'London'] countries = {} for key, value in zip(keys, values): countries[key] = value print(countries) # {'India': 'New Delhi', 'United States': 'Washington D.C.', 'United Kingdom': 'London'}