Skip to content
Advertisement

How can I handle only the first part of my value list of a dictionary in Python?

I have a dictionary which has a list of values and I want to use only the first value of each pair before the first comma. Is that possible? This is the format of my dictionary values

If you came across anything similar please write it down

Advertisement

Answer

The output you expect is unclear. If you want a dictionary in which you only keep the first item of the sublist, use a dictionary comprehension:

Assuming dic1 the input.

dic2 = {k:v[0][0] for k,v in dic1.items()}
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement