Fetch value from python dictionary and pass one by one - python

I have dictionary as mentioned below.
a={'name':['test1','test2'],'regno':['123','345'],'subject':
['maths','science'],'standard':['3','4']}
I need verify below things.
Each values count dictionary should be match.
Fetch the values from each keys one by one and pass it to my other function one by one.
name = 'test1' regno = '123' subject='maths' standard='3'
name = 'test2' regno = '345' subject='science' standard='4'
I have tried using below code but i am stuck here to find out exact way.
a={'name':['test1','test2'],'regno':['123','345'],'subject':['maths','science'],'standard':['3','4']}
lengths = [len(v) for v in a.values()]
if (len(set(lengths)) <= 1) == True:
print('All values are same')`
else:
print('All values are not same')
Need your help to fetch values one by one from each keys and pass it to a function.

Try looping over your dictionary items and then over the lists in values:
for key, vals_list in a.items():
if len(set(vals_list)) <= 1:
print(f'{key}: All values are same!')
# Will do nothing if `vals_list` is empty
for value in vals_list:
your_other_func(value)

You can get it done this way:
a={'name':['test1','test2'],'regno':['123','345'],'subject':
['maths','science'],'standard':['3','4']}
w = [{'name':a['name'][i], 'regno':a['regno'][i], 'standard':a['standard'][i]} for i
in range(len(a['name']))]
for x in range(len(w)):
#your_func(w[x]['name'], w[x]['reno'], w[x]['standard'])
print(w[x]['name'], w[x]['regno'], w[x]['standard'])

I would rebuild a into a list of dictionaries, and then use dict-unpacking to dynamically give the dictionary to the function instead:
def func(name, regno, subject, standard):
print("name={}, regno={}, subject={}, standard={}".format(name, regno, subject, standard))
a={'name':['test1','test2'],'regno':['123','345',],'subject':
['maths','science'],'standard':['3','4']}
new_a = [dict(zip(a.keys(), x)) for x in list(zip(*a.values()))]
print(new_a)
for d in new_a:
func(**d)
Output:
[{'name': 'test1', 'regno': '123', 'subject': 'maths', 'standard': '3'}, {'name': 'test2', 'regno': '345', 'subject': 'science', 'standard': '4'}]
name='test1', regno='123', subject='maths', standard='3'
name='test2', regno='345', subject='science', standard='4'

Related

check if a list of dict is in ascending value and return the incorrect dict

Dict below is just for example purpose:
a = [{'name':'sally','age':'31'},{'name':'greg','age':'30'},{'name':'josh','age':'32'},{'name':'bobby','age':'33',]
The order of the dict need to follow the age. So i need to check if the dict is in order or not and print their 'name' if the age not in order.
What i did so far was sorted out the list of dict according to their age (ascending)
sorted_age = sorted(a, key=lambda d: d['age'])
and compare both dict to see if they are equal or not.
if a == sorted_age:
continue
else:
print(False)
I need to print 'sally' and 'greg' as their age are not in order. and i dont know how to print their name
You can use zip:
[(a, b) for a, b in zip(data, data[1:]) if a['age'] > b['age']]
[({'name': 'sally', 'age': '31'}, {'name': 'greg', 'age': '30'})]
Just iterate through the dicts:
for da, ds in zip(a, sorted_age):
if da != ds:
print(da['name'])
print('are not in order')

Get a specific value from a list of dictionaries

I want to get the value a specific value '1222020' which has 'Expiration' as a key:
The 'Expiration' key can be placed at any position.
input :
my_list=[{'Key': 'Expiration', 'Value': '12122020'}, {'Key': 'Name', 'Value': 'Config Test 2'}]
my solution:
res = [sub['Value'] for sub in my_list if sub['Key'] =='Expiration' ]
print(res)
Sometimes the tag 'Expiration' is not present.
How to Handle that and avoid NoneType Object error
If you could re-organize your data like so,
custom_dict = {'Expiration': '12122020', 'Name': 'Config Test 2'}
Then, you could write the code like this,
def get_key_value_from_dictionary_search(dict_data, key_search_phrase, value_search_phrase):
for k,v in dict_data.items():
if k is key_search_phrase and v is value_search_phrase:
return k, v
_k, _v = get_key_value_from_dictionary_search(custom_dict, "Expiration", "12122020")
print("Key : {}\nValue : {}".format(_k, _v))
If the Expiration key isn't present, your res evaluates to an empty list. So if you just check for the presence on an empty list, you'll know if Expiration was in there to begin with.
def get_result(lst, default="99999999"):
res = [sub['Value'] for sub in lst if sub['Key'] == 'Expiration']
if res:
# there is something in the list, so return the first thing
return res[0]
else:
# the list is empty, so Expiration wasn't in lst
return default
print(get_result(my_list))

Python - Get dictionary element in a list of dictionaries after an if statement

How can I get a dictionary value in a list of dictionaries, based on the dictionary satisfying some condition? For instance, if one of the dictionaries in the list has the id=5, I want to print the value corresponding to the name key of that dictionary:
list = [{'name': 'Mike', 'id': 1}, {'name': 'Ellen', 'id': 5}]
id = 5
if any(m['id'] == id for m in list):
print m['name']
This won't work because m is not defined outside the if statement.
You have a list of dictionaries, so you can use a list comprehension:
[d for d in lst if d['id'] == 5]
# [{'id': 5, 'name': 'Ellen'}]
new_list = [m['name'] for m in list if m['id']==5]
print '\n'.join(new_list)
This will be easy to accomplish with a single for-loop:
for d in list:
if 'id' in d and d['in'] == 5:
print(d['name'])
There are two key concepts to learn here. The first is that we used a for loop to "go through each element of the list". The second, is that we used the in word to check if a dictionary had a certain key.
How about the following?
for entry in list:
if entry['id']==5:
print entry['name']
It doesn't exist in Python2, but a simple solution in Python3 would be to use a ChainMap instead of a list.
import collections
d = collections.ChainMap(*[{'name':'Mike', 'id': 1}, {'name':'Ellen', 'id': 5}])
if 'id' in d:
print(d['id'])
You can do it by using the filter function:
lis = [ {'name': 'Mike', 'id': 1}, {'name':'Ellen', 'id': 5}]
result = filter(lambda dic:dic['id']==5,lis)[0]['name']
print(result)

Delete entry from a dictionary of dictionaries

I have a dictionary of dictionaries..
self.books = {}
newbook = {'Quantity': '1', 'Price': '1.58', 'Id': '1006', 'Title': 'Book1', 'Status': 'Out'}
I add orders like so:
self.books[newbook['Price']] = newbook
To show all the book entries:
for books in self.books.values():
print("{0}".format(order))
{'Quantity': '3', 'Price': '7.56', 'Id': '1003', 'Title': 'To kill a mock', 'Status':'In'}
{'Quantity': '7', 'Price': '2.9', 'Id': '1002', 'Title': 'LOTR', 'Status':'In'}
{'Quantity': '2', 'Price': '5.45', 'Id': '1001', 'Title': 'HPATFS', 'Status':'Out'}
{'Quantity': '12', 'Price': '11.57', 'Id': '1004', 'Title': 'Best book ever', 'Status':'Out'}
Unfortunately I can't delete a book entry based on Id or Price or Status? I've tried del or pop, but the entry still remains.
for k, v in self.books.items():
if k == books['Id']:
# del self.books[k]
self.books.pop(books['Id'], None)
First and foremost, you can't use values that you are not sure are unique as keys for a dictionary. Dictionaries are indexed by their key and indexing has to be unique. In fact, the language doesn't even allow you to try to add two entries with the same value. Adding the second item simply overwrites the first:
>>> d = {'a':1}
>>> d.update({'a':2})
>>> print(d)
{'a': 2}
So, you definitely should use the 'Id' value as the key for your dictionary, assuming your Ids are in fact unique.
Because of this, the rest of your question boils down to finding a dictionary entry by it's value instead of it's key. For this, you have to define a behavior for when the dict contains multiple entries with that value. I'm going to assume you want a list of all Ids with the specified pricetag.
The only way I can think of for this that accounts for the possibility of multiple entries with the same value is simply iterating over dict:
def findKeysByValue(dic, value):
match_keys = []
for key, val in dic.items():
if val == value:
match_keys.append(key)
return match_keys
(use dic.iteritems() instead for Python 2.X)
Which yields for example:
>>> d = {'k1':1,'k2':2,'k3':3, 'k4':2, 'k5':2}
>>> findKeysByValue(d,2)
['k5', 'k2', 'k4']
Once you know the key of the book that interests you, you can apply all dictionary operations on that key.
There are two different problems with the last part of your code. Firstly, pop takes a key from your dictionary as an argument, not a value. Also, this code produces a RuntimeError: dictionary changed size during iteration, because you iterate over the dictionary itself and remove an item every cycle. You can circumvent this by iterating over a copy of your dict, i.e.:
copy_dict = self.books.copy()
for k in copy_dict:
if k == books['Id']:
self.books.pop(k, None)
Depending on the size of self.books though, this may not be suitable, because you have double the space requirement.

How to check if list item is in dictionary. If so, print the data associated with it

I have a list of names called names. I also have 2 dictionaries that contain a list of nested dictionaries that both have the key names and other data associated with the name. What I want to do is check that the name from the list is in one of the 2 dictionaries, if so, print the data associated only with that name. I can't find any of this stuff in the python docs
names = ['barry','john','george','sarah','lisa','james']
dict1 = {'results':[{'name':'barry','gpa':'2.9','major':'biology'},
{'name':'sarah','gpa':'3.2','major':'economics'},
{'name':'george','gpa':'2.5','major':'english'}]}
dict2 = {'staff':[{'name':'john','position':'Lecturer','department':'economics'},
{'name':'lisa','position':'researcher','department':'physics'},
{'name':'james','position':'tutor','department':'english'}]}
for x in names:
if x in dict1:
print gpa associated with the name
elif x in dict2:
print position associated with the name
The structure you're using for the two dicts isn't very optimal - each contains only a single element, which is a list of the relevant data for each person. If you can restructure those with a separate element per person using the name as a key, this becomes a trivial problem.
dict1 = {'barry': {'gpa':'2.9','major':'biology'},
'sarah': {'gpa':'3.2','major':'economics'},
'george': {'gpa':'2.5','major':'english'}}
dict2 = {'john': {'position':'Lecturer','department':'economics'},
'lisa': {'position':'researcher','department':'physics'},
'james': {'position':'tutor','department':'english'}}
Since it appears you can't get the data in this format, you'll have to convert it:
dict_results = dict((d['name'], d) for d in dict1[results])
if name in dict_results:
print dict_results[name]['gpa']
for _name in names:
if _name in [person['name'] for person in dict1['results']]: pass
elif _name in [person['name'] for person in dict2['staff']]:pass
something like that at least
This should get you an idea:
for name in names:
print name, ":"
print "\t", [x for x in dict2["staff"] if x["name"] == name]
print "\t", [x for x in dict1["results"] if x["name"] == name]
prints
barry :
[]
[{'major': 'biology', 'name': 'barry', 'gpa': '2.9'}]
john :
[{'department': 'economics', 'position': 'Lecturer', 'name': 'john'}]
[]
george :
[]
[{'major': 'english', 'name': 'george', 'gpa': '2.5'}]
sarah :
[]
[{'major': 'economics', 'name': 'sarah', 'gpa': '3.2'}]
lisa :
[{'department': 'physics', 'position': 'researcher', 'name': 'lisa'}]
[]
james :
[{'department': 'english', 'position': 'tutor', 'name': 'james'}]
[]
If you get this data from a database, you should probably rather work on the SQL frontier of the problem. A database is made for operations like that.
names = ['barry','john','george','sarah','lisa','james']
dict1 = {'results':[{'name':'barry','gpa':'2.9','major':'biology'},
{'name':'sarah','gpa':'3.2','major':'economics'},
{'name':'george','gpa':'2.5','major':'english'}]}
dict2 = {'staff':[{'name':'john','position':'Lecturer','department':'economics'},
{'name':'lisa','position':'researcher','department':'physics'},
{'name':'james','position':'tutor','department':'english'}]}
import itertools
for x in itertools.chain(dict1['results'], dict2['staff']):
for n in names:
if n in x.values():
print x
or the 2nd part could be:
ns = set(names)
for x in itertools.chain(dict1['results'], dict2['staff']):
if x['name'] in ns:
print x

Categories

Resources