Search code examples
python-3.xlistdictionaryintegertuples

How to remove list datatype for values inside a dictionary in python?


I want to remove the list data type for integers values inside a dictionary in python.

Below is the input dictionary :

input_dictionary = {'key1': [21477], 'key2': [92], 'key3': [92], 'key4': [197], 'key5': [197]}

expected output_dictionary = {'key1': 21477, 'key2': 92, 'key3': 92, 'key4': 197, 'key5': 197}

How to frame the python code in order to get expected output_dictionary as below :

{'key1': 21477, 'key2': 92, 'key3': 92, 'key4': 197, 'key5': 197}

Thanking you in advance for your help.


Solution

  • You can do so in a simple dict comprehension:

    output = {k: v[0] for k, v in input_dictionary.items()}