Home > Enterprise >  Move Dictionary Keys to lis for particular values
Move Dictionary Keys to lis for particular values

Time:10-25

I trying to make this code work:

civil_freq= {   '430.00': ['aaa'], 
                '430.02': ['aaa'], 
                '430.04': ['aaa'], 
                '430.06': ['bbb'], 
                '430.08': ['bbb'], 
                '430.10': ['bbb'], 
                '430.12': ['none'], 
                '430.14': ['none']}
person_freq=[]
person = 'bbb'
for key in civil_freq:
    if civil_freq[key] == person:
        person_freq.append(civil_freq.get(key))

print(person_freq)

it return empty list, but I need smth like

['430.06', '430.08', '430.10']

CodePudding user response:

Issue: You're storing the persons names in a list (within your civil_freq dictionary) but comparing it with a string (variable person). This comparison won't work.

Try this:

person = ["bbb"]
for k, v in civil_freq.items():
    if v == person:
        person_freq.append(k)

print(person_freq)

or change the values within your dictionary from lists to strings!

  • Related