Reputation: 45
d1 = {'name': 'Sagar','age': 25}
d2 = {'name': 'Sassdr', 'age':122}
d3 = {'name': 'Saga23weer', 'age':123344}
d4 = {'name': '2133Sagar', 'age':14322}
ch = input("Enter your value: ")
How can I search inputted value from these dictionaries? and if found value then it returns Found else return not found.
Upvotes: 0
Views: 1072
Reputation: 81
you need to do:
get the values of dict;
search all dicts;
mark result as "Found" if matches.
for step 1:
dict.values()
for step 2:
there's many way to combine all dicts, as everybody gives.
you can pick all values first to set a new list, then search if your input matches like this:
# combine all dicts
d = d1.values() + d2.values() +d3.values() + d4.values()
# judge if matches
if ch in d:
# do something
hope this code can release your confuse.
Upvotes: 0
Reputation: 7
You can use following code
Python2
def IsFound():
list_dict = [d1, d2, d3, d4]
values_list = []
for each in list_dict:
values_list += each.values()
ch = input('enter your value')
if ch in values_list:
return 'Found'
else:
return 'Not Found'
Python3
def IsFound():
dict = {**d1, **d2, **d3, **d4}
ch = input('enter your value')
if ch in dict.values():
return 'Found'
else:
return 'Not Found'
Upvotes: 0
Reputation: 606
Make a list of dictionaries and search in it:
d1 = {'name': 'Sagar','age': 25}
d2 = {'name': 'Sassdr', 'age':122}
d3 = {'name': 'Saga23weer', 'age':123344}
d4 = {'name': '2133Sagar', 'age':14322}
d = [d1,d2,d3,d4]
def check(ch):
for entry in d:
if entry["name"] == ch:
return("found")
return ("Not found")
while True:
ch = input("Enter your value: ")
if ch == "stop":
break
print(check(ch))
Output:
>>>
Enter your value: Sagar
found
Enter your value: Someone
Not found
Enter your value: 2133Sagar
found
Enter your value: stop
Upvotes: 1
Reputation: 1374
The effect you want is called key swap. This snippet is the implementation:
def keyswap(yourdict):
cache = {}
for i in yourdict.keys():
cache[yourdict[i]] = i
for i in cache.keys():
yourdict[i] = cache[i]
del cache
It keyswaps inplace.
Upvotes: 0
Reputation: 4092
Why are a search value in different dictionary rather than in one??
Try this
merge all dictionary in one
d5 = {**d1, **d2, **d3, **d4}
and then check
if ch in d5 .values():
print "Found"
else:
print "Not Found"
Upvotes: 2