Instead of using any external module, you can use some logic and do it without any module:
track={} if intr not in track: track[intr]=1 else: track[intr]+=1
Code example:
There is a template for these types of list tasks:
So you have a list:
a=[(2006,1),(2007,4),(2008,9),(2006,5)]
And you want to convert this to a dict as the first element of the tuple as a key and the second element of the tuple. sort of:
{2008: [9], 2006: [5], 2007: [4]}
But there is a trick that you also want those keys that have different meanings, but the keys are the same as (2006,1) and (2006,5), the same, but the meanings are different. you want these values ββto be added with only one key expected output:
{2008: [9], 2006: [1, 5], 2007: [4]}
for this type of task, we do something like this:
first create a new dict, then we follow this pattern:
if item[0] not in new_dict: new_dict[item[0]]=[item[1]] else: new_dict[item[0]].append(item[1])
So, first we check if the key is in the new dict, and if it already adds the value of the duplicate key to its value:
full code:
a=[(2006,1),(2007,4),(2008,9),(2006,5)] new_dict={} for item in a: if item[0] not in new_dict: new_dict[item[0]]=[item[1]] else: new_dict[item[0]].append(item[1]) print(new_dict)
output:
{2008: [9], 2006: [1, 5], 2007: [4]}