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
  Find (each) element from a list in a file tester_V 3 276 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 308 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 491 Jul-01-2022, 10:52 PM
Last Post: Pedroski55
  How to solve this simple problem? Check if cvs first element is the same in each row? thesquid 2 506 Jun-14-2022, 08:35 PM
Last Post: thesquid
  How to find the second lowest element in the list? Anonymous 3 561 May-31-2022, 01:58 PM
Last Post: Larz60+
  Dictionary from a list failed, help needed leoahum 7 920 Apr-28-2022, 06:59 AM
Last Post: buran
  how to assign items from a list to a dictionary CompleteNewb 3 814 Mar-19-2022, 01:25 AM
Last Post: deanhystad
  How to check if a list is in another list finndude 4 992 Jan-17-2022, 05:04 PM
Last Post: bowlofred
  Python, how to manage multiple data in list or dictionary with calculations and FIFO Mikeardy 8 1,287 Dec-31-2021, 07:47 AM
Last Post: Mikeardy
  Class-Aggregation and creating a list/dictionary IoannisDem 1 1,124 Oct-03-2021, 05:16 PM
Last Post: Yoriz

Forum Jump:

User Panel Messages

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