0

This is what I have:

names = ['Bob', 'Mike', 'Nia', 'Tim', 'Holly', 'Liam', 'Dave']
temperatures = ['-0.3', '-0.6', '-0.8', '-0.2', '-1.3', '-2.1', '-0.4']

main_dictionary = {'Bob' : '-0.3', 'Mike': '-0.6', 'Nia' : '-0.8', 'Tim':'-0.2', 'Holly':'-1.3', 'Liam':'-2.1', 'Dave':'-0.4'}

I'm trying to produce a dictionary by comparing it to the list of selected_names against the main_dictionary to produce the updated_dictionary

selected_names = ['Nia', 'Holly', 'Liam', 'Dave']

updated_dictionary = {'Nia' : '-0.8',  'Holly':'-1.3', 'Liam':'-2.1', 'Dave':'-0.4'}

So far in my project Ive compared many lists using .intersection but can seem to work out how to apply it to a dictionary.

3 Answers3

1

Probably a simple dict comprehension is enough:

updated_dictionary = {x: main_dictionary[x] for x in selected}

If selected may contain invalid keys, you could explicitely ignore them:

updated_dictionary = {x: main_dictionary[x] for x in selected if x in main_dictionary[x]}

See also Create a dictionary with list comprehension and Filter dict to contain only certain keys?

ypnos
  • 50,202
  • 14
  • 95
  • 141
0

Try this

main_dictionary = {'Bob' : '-0.3', 'Mike': '-0.6', 'Nia' : '-0.8', 'Tim':'-0.2', 'Holly':'-1.3', 'Liam':'-2.1', 'Dave':'-0.4'}
selected_names = ['Nia', 'Holly', 'Liam', 'Dave']

newd = {k:v for k,v in main_dictionary.items() if k in selected_names}

print(newd)
mama
  • 2,046
  • 1
  • 7
  • 24
  • Your solution is quite inefficient, as for each key in `main_dictionary` you have to go through the whole list of `selected_names`. – ypnos Dec 02 '21 at 14:21
  • python is an inefficient language – mama Dec 02 '21 at 14:22
  • @ypnos if a key is missing this approach won't throw an error.. so it has at least one advantage ;) – Hansanho Dec 02 '21 at 14:24
  • @ypnos it works great but by inefficient I guess you mean slow? If my project doesn't need to be quick does this matter? – Barry Marples Dec 02 '21 at 14:25
  • 1
    Yes if your `selected_names` is big or `main_dictionary` is big, then it becomes an issue. The question is, why start writing inefficient code in the first place? – ypnos Dec 02 '21 at 14:26
-1

Try this:

names = ['Bob', 'Mike', 'Nia', 'Tim', 'Holly', 'Liam', 'Dave']
temperatures = ['-0.3', '-0.6', '-0.8', '-0.2', '-1.3', '-2.1', '-0.4']

main_dictionary = {
    'Bob': '-0.3',
    'Mike': '-0.6',
    'Nia': '-0.8',
    'Tim': '-0.2',
    'Holly': '-1.3',
    'Liam': '-2.1',
    'Dave': '-0.4'
}

selected_names = ['Nia', 'Holly', 'Liam', 'Dave']
output = {
    'Nia': '-0.8',
    'Holly': '-1.3',
    'Liam': '-2.1',
    'Dave': '-0.4'
}
updated_dictionary = {k: v for k, v in main_dictionary.items() if k in selected_names}

if __name__ == '__main__':
    print("main", main_dictionary)
    print("updated", updated_dictionary)
    print(updated_dictionary == output)

# It produces:
# {'Nia': '-0.8', 'Holly': '-1.3', 'Liam': '-2.1', 'Dave': '-0.4'}
# updated {'Nia': '-0.8', 'Holly': '-1.3', 'Liam': '-2.1', 'Dave': '-0.4'}
# True
Winter Squad
  • 169
  • 7