Lists and Dictionaries

682
7
Jump to solution
08-27-2019 12:34 PM
JoeBorgione
MVP Emeritus

I have two lists of tuples in the form of:

list1 =  [('84116', 1750),('84116', 1774),('84116', 1783),('84116',1792)]
list2 = [('84116', 1783),('84116', 1792),('84116', 1847),('84116', 1852),('84116', 1853)]‍‍‍‍‍‍

 

according to Python : How to convert a list to dictionary ? – thispointer.com , I should be able to create a dictionary of a list of tuples like this:

list1Dict = dict(list1)
list2Dict = dict(list2)

list1Dict
{'84116': 1792}

list2Dict
{'84116': 1853}‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

However, as you can see, in both cases my respective dictionaries only have one pair of keys:values - The last tuple of each list.

Question 1:  how do I get my complete list of tuples into a dictionary form?

The objective is to get either a list or a dictionary of unique tuples/unique key:value pairs in a list or dictionary.

Question 2: how would I accomplish this:

list1 =  [('84116', 1750),('84116', 1774),('84116', 1783),('84116',1792)]
list2 = [('84116', 1783),('84116', 1792),('84116', 1847),('84116', 1852),('84116', 1853)]‍‍

uniqueList = [('84116', 1750),('84116', 1774),('84116', 1847),('84116', 1852),('84116', 1853)]‍‍

uniqueDict = {'84116':1750,'84116':1774,'84116':1847,'84116':1852,'84116':1853}‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

edited after some more fumbling....

Found something puzzling with respect to question # 2.

listofTuples = [("abc" , 11), ("84116" , 12), ("84116" , 13),("84116" , 22),("84116" , 90)]
studentsDict = dict(listofTuples)

studentsDict
{'abc': 11, '84116': 90}‍‍‍‍‍

 For some reason python will skip quoted numbers until the very end?

edited again:

 

You can't have multiple instances of the same key in a dictionary.  I could have one key of '84116' with mulitple values.....

That should just about do it....
0 Kudos
1 Solution

Accepted Solutions
JoshuaBixby
MVP Esteemed Contributor

Take your list of tuple, convert it to a set to get unique entries, and then convert it back to a list:

list(set(list2))

View solution in original post

7 Replies
JoshuaBixby
MVP Esteemed Contributor

Take your list of tuple, convert it to a set to get unique entries, and then convert it back to a list:

list(set(list2))
JoeBorgione
MVP Emeritus

I get my unique values by first combining the two lists:

list(set(list1+list2))

 [('84116', 1750),
 ('84116', 1792),
 ('84116', 1774),
 ('84116', 1783),
 ('84116', 1853),
 ('84116', 1847),
 ('84116', 1852)]
That should just about do it....
0 Kudos
DanPatterson_Retired
MVP Emeritus

Joe... but your final result looks like this? or do you only have 1 key.

{'84116': [1750, 1774, 1783, 1792, 1847, 1852, 1853]}

There is a solution of multiple keys if you don't have one

JoeBorgione
MVP Emeritus

Dan-  I would very much like to have a result such as what you show.  The goal is to get the number of unique values associated with each key; more or less a summary table.

These data represent zipcodes (keys) and customers (values).  I need to keep a year to date tally of unique customers per zip, per month.  The number of total customers served per month changes, which is a pretty easy additive sort of thing, month to month.

In my example back at the top List1 is one zip and one month worth of customers while List2 is the same zip,and the  next month.  The total number of customers served in that zipcode  for the two months is 9, but the total unique customers is 7...

That should just about do it....
0 Kudos
DanPatterson_Retired
MVP Emeritus

Joe Borgione‌ not sure why you need a dictionary but... 

z = np.asarray([str(i) for i in np.random.randint(84110, 84120, 100)])
c = np.random.randint(1750, 1780, 100)
dt = [('ZipCode', 'U8'), ('Customers', '<i4')]
keys = np.unique(data['ZipCode'])
kv = {i : data['Customers'][data['ZipCode'] == i].tolist() for i in keys}

{'84110': [1757, 1755, 1766, 1753, 1755, 1774, 1760, 1765, 1772, 1776, 1761, 1762],
 '84111': [1753, 1762, 1760, 1769, 1766, 1761, 1766, 1764, 1772, 1760],
 '84112': [1761, 1765, 1766, 1760, 1770, 1752, 1763, 1761, 1770, 1763, 1774],
 '84113': [1762, 1766, 1773, 1751, 1778, 1762, 1756, 1751],
 '84114': [1770, 1759, 1774, 1760, 1757, 1775, 1759, 1763, 1775],
 '84115': [1756, 1766, 1753, 1754, 1767, 1758, 1768, 1775, 1769, 1774, 1772, 1777, 1764, 1778, 1750],
 '84116': [1759, 1776, 1762, 1778, 1765, 1763, 1763, 1774, 1775, 1770],
 '84117': [1762, 1750, 1769, 1756, 1759, 1763, 1779, 1766],
 '84118': [1753, 1776, 1752, 1775, 1758, 1770, 1761, 1756],
 '84119': [1771, 1768, 1750, 1764, 1768, 1762, 1759, 1761, 1759]}
JoeBorgione
MVP Emeritus

hmmm... let me work with this....

Thanks.

That should just about do it....
0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Joe, you can also use a defaultdict paired with set:

>>> from collections import defaultdict
>>>
>>> d = defaultdict(set)
>>> l = [
...     ("foo", 1),
...     ("bar", 1),
...     ("foo", 3),
...     ("bar", 2),
...     ("foo", 1),
...     ("bar", 3),
...     ("foo", 3),
...     ("bar", 1)
... ]
>>>
>>> for k,v in l:
...   d[k].add(v)
...
>>> d
defaultdict(<class 'set'>, {'foo': {1, 3}, 'bar': {1, 2, 3}})
>>>