I realize this is a basic question but I cannot get this to work.
I have the following tuple:
t = (('apple', 'banana'), ('apple', 'carrot'), ('banana', 'carrot'))
I want the first item within every tuple in t to be unique, and to merge the second item of every duplicate together into a dictionary.
t = {('apple': 'banana', 'carrot'), ('banana': 'carrot')}
Not surprisingly, this doesn’t work:
t = dict(zip(set(t[0]),t[1]))
This is the result:
{'apple': 'apple', 'banana': 'carrot'}
I think, you should use a
dicthere (or rather adefaultdict). Wherekeyof thedictwould be the first element of your inner tuples –tup[0].This is what you want: