Friday, December 16, 2016


Updating a dictionary value from various lists.

In 3.5.1

The same happens for 2.6.6

Here, we are updating the list values of a dictionary key with lists - list1 and list2


We can aggregate values (for a given key) into a list in a dictionary with the 'defaultdict' dictionary-like object from the collections module.

In both 3.5.1 and 2.6.6. Click to enlarge the below screenshot :-



Here, we have a list s whose elements are tuples comprising of a generic drug name and one of its brand name. With generic name as the key, we need to collect its corresponding drug brand names in a list.

If we also want to keep a count the number of drug names for a given generic name we can do the following:- (Click on the image to expand)



And, to get the count we just need to do d[k].[-1]