Python Forum
check if element is in a list in a dictionary value
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
check if element is in a list in a dictionary value
#1
how can I check if an element is in a list in a dictionary value? for instance

how can I get this code to output "Found it"

a = "Doors"

dict = {"a":["Doors","Windows"],"b":"Stairs"}

if a in dict.values():
print("Found it")
else:
print("sorry")

Thank you
Reply
#2
Well, remember that the value you're getting that contains what you're looking for is a list, so you need to look inside that. Can you have nested containers containing the value? Recursion would help in that case.
Reply
#3
(May-11-2022, 04:07 PM)ndc85430 Wrote: Well, remember that the value you're getting that contains what you're looking for is a list, so you need to look inside that. Can you have nested containers containing the value? Recursion would help in that case.

something like this?

a = "Doors"

dict = {"a":["Doors","Windows"],"b":"Stairs"}

for val in dict.values():
if a in val:
print("Found it")
else:
print("sorry")
Reply
#4
Hello,
(May-11-2022, 04:18 PM)ambrozote Wrote: something like this?
You can use for/else loop:
a = "Doors"
dict = {"a":["Doors","Windows"],"b":"Stairs"}

for val in dict.values():
    if a in val:
        print("Found it")
        break
else:
    print("sorry")
Another possible approach is:
a = "Doors"
dict = {"a":["Doors","Windows"],"b":"Stairs"}
if any(val for val in dict.values() if a in val):
    print("Found it")
else:
    print("sorry")
I speak Python but I don't speak English (I just read it a little). If I express myself badly, please blame the translator^^.
Reply
#5
import itertools

x = {
    2:[2, 4, 6, 8],
    3:[3, 6, 9, 12],
    5:[5, 10, 15, 20]
}

values = set(itertools.chain(*x.values()))  # Get set containing all values from dictionary
for i in range(10):
    print(i, i in values)
Output:
0 False 1 False 2 True 3 True 4 True 5 True 6 True 7 False 8 True 9 True
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with to check an Input list data with a data read from an external source sacharyya 3 318 Mar-09-2024, 12:33 PM
Last Post: Pedroski55
  [solved] list content check paul18fr 6 616 Jan-04-2024, 11:32 AM
Last Post: deanhystad
  Dictionary in a list bashage 2 496 Dec-27-2023, 04:04 PM
Last Post: deanhystad
  filtering a list of dictionary as per given criteria jss 5 598 Dec-23-2023, 08:47 AM
Last Post: Gribouillis
  Sort a list of dictionaries by the only dictionary key Calab 1 452 Oct-27-2023, 03:03 PM
Last Post: buran
  list in dicitonary element problem jacksfrustration 3 625 Oct-14-2023, 03:37 PM
Last Post: deanhystad
  How to add list to dictionary? Kull_Khan 3 951 Apr-04-2023, 08:35 AM
Last Post: ClaytonMorrison
  Find (each) element from a list in a file tester_V 3 1,157 Nov-15-2022, 08:40 PM
Last Post: tester_V
  Сheck if an element from a list is in another list that contains a namedtuple elnk 8 1,738 Oct-26-2022, 04:03 PM
Last Post: deanhystad
  Membership test for an element in a list that is a dict value for a particular key? Mark17 2 1,162 Jul-01-2022, 10:52 PM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

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