Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Some help with Dictionaries
#2
The dictionary itself doesn't have any features that allow you to search a key based on a value. All you can do is loop over every key/value and keep track of keys where the value meets your criteria. As the dictionary grows large, this operation will become more expensive.



a_dic = {
    "a" :   [":D", ":)"],
    "b" :   ["lol", "XD"],
    "c" :   ["lol", ">:"],
    "d" :   [":)", "XD"]
}

search_item = ":)"

match_keys = [k for k,v in a_dic.items() if search_item in v]
print(match_keys)
Output:
['a', 'd']
Oshadha likes this post
Reply


Messages In This Thread
Some help with Dictionaries - by Oshadha - Jan-19-2021, 06:23 AM
RE: Some help with Dictionaries - by bowlofred - Jan-19-2021, 06:46 AM
RE: Some help with Dictionaries - by Oshadha - Jan-19-2021, 06:57 AM
RE: Some help with Dictionaries - by DeaD_EyE - Jan-19-2021, 08:48 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020