note: solutions submitted before are more pythonic than mine. but in my opinon, lines that i've written before are easier to understand. i simply will create a dictionary, then will add mail adresses as key and the indexes as value.
first declare an empty dictionary.
>>> dct = {}
then iterate over mail adresses (m
) and their indexes (i
) in mailAddressList
and add them to dictionary.
>>> for i, m in enumerate(mailAddressList):
... if m not in dct.keys():
... dct[m]=[i]
... else:
... dct[m].append(i)
...
now, dct
looks liike this.
>>> dct
{'support@plastroltech.com': [5], 'webdude@plastroltech.com': [2],
'chip@plastroltech.com': [0], 'spammer@example.test': [1, 3, 4]}
there are many ways to grab the [1,3,4]
. one of them (also not so pythonic :) )
>>> [i for i in dct.values() if len(i)>1][0]
[1, 3, 4]
or this
>>> [i for i in dct.items() if len(i[1])>1][0] #you can add [1] to get [1,3,4]
('spammer@example.test', [1, 3, 4])