0

From the following dictionary structure, how do I extract and print email addresses? E.g. I want to see 'smauel.david@gmail' 4, 'sdusa@yahoo.com' 1, etc.

dict_items([('10:04:14', 1), ('3', 6), ('Thu', 6), ('19:51:21', 1),
('2008',27),  ('From', 27), ('11:35:08', 1), ('5', 1),
('sntp@hotmail.com', 3), ('Jan', 27), ('15:46:24', 1), ('14:50:18',
1),  ('11:37:30', 1), ('18:10:48', 1), ('17:07:00', 1), ('09:05:31',
1),  ('10:38:42', 1), ('sdusa@yahoo.com', 1),
('samuel.david@gmail.com', 4) ])
styvane
  • 59,869
  • 19
  • 150
  • 156
Samuel
  • 181
  • 1
  • 9

3 Answers3

2

Use dict comprehensions

d = dict([('10:04:14', 1), ('3', 6), ('Thu', 6), ('19:51:21', 1),('2008',27),  ('From', 27), ('11:35:08', 1), ('5', 1),('sntp@hotmail.com', 3), ('Jan', 27), ('15:46:24', 1), ('14:50:18',1),  ('11:37:30', 1), ('18:10:48', 1), ('17:07:00', 1), ('09:05:31',1),  ('10:38:42', 1), ('sdusa@yahoo.com', 1),('samuel.david@gmail.com', 4) ])

{email:val for email, val in d.items() if '@' in email }

output:

 {'sdusa@yahoo.com': 1, 'samuel.david@gmail.com': 4, 'sntp@hotmail.com': 3}
styvane
  • 59,869
  • 19
  • 150
  • 156
  • Brilliant, short and simple. It worked. I love this community of developers and trust their expertise. It was a brain freeze and just couldn't think. You saved my lot of time. I hope to stay in touch for professional advice. A million thanks. – Samuel Mar 29 '15 at 02:47
1

You can iterate the keys, and grab only those that contain @ (assuming that all your keys are either hours or email addresses):

d = dict([('10:04:14', 1), ('3', 6), ('Thu', 6), ('19:51:21', 1), ('2008',27),  ('From', 27), ('11:35:08', 1), ('5', 1), ('sntp@hotmail.com', 3), ('Jan', 27), ('15:46:24', 1), ('14:50:18', 1),  ('11:37:30', 1), ('18:10:48', 1), ('17:07:00', 1), ('09:05:31', 1),  ('10:38:42', 1), ('sdusa@yahoo.com', 1), ('samuel.david@gmail.com', 4) ])

for key in d:
    if "@" in key:
        print(key)

OUTPUT

sntp@hotmail.com
samuel.david@gmail.com
sdusa@yahoo.com
Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
  • Brilliant, short and simple. It worked. I love this community of developers and trust their expertise. It was a brain freeze and just couldn't think. You saved my lot of time. I hope to stay in touch for professional advice. A million thanks. – Samuel Mar 29 '15 at 02:47
  • @Samuel are you a bot ? :) – Nir Alfasi Mar 29 '15 at 02:50
  • 1
    Yes indeed now I am back on track. Thanks again. Have a nice weekend. – Samuel Mar 29 '15 at 03:01
0

Assuming:

>>> d=dict([('10:04:14', 1), ('3', 6), ('Thu', 6), ('19:51:21', 1),
... ('2008',27),  ('From', 27), ('11:35:08', 1), ('5', 1),
... ('sntp@hotmail.com', 3), ('Jan', 27), ('15:46:24', 1), ('14:50:18',
... 1),  ('11:37:30', 1), ('18:10:48', 1), ('17:07:00', 1), ('09:05:31',
... 1),  ('10:38:42', 1), ('sdusa@yahoo.com', 1),
... ('samuel.david@gmail.com', 4) ])

If the address are of the type you show, you can use filter on the keys:

>>> list(filter(lambda s: '@' in s, d.keys()))
['sdusa@yahoo.com', 'samuel.david@gmail.com', 'sntp@hotmail.com']

If, however, you might have more complete strings representing the RFC822 email address of 'Comment' <address> type (or a mixture) you might want to use email.utils.parseaddr() to separate the comment (or real name) from the actual address:

>>> from email.utils import parseaddr
>>> parseaddr('"Santa" <Santa@np.org>')
('Santa', 'Santa@np.org')

So then:

>>> d=dict([('10:04:14', 1), ('3', 6), ('Thu', 6), ('19:51:21', 1),
... ('2008',27),  ('From', 27), ('11:35:08', 1), ('5', 1),
... ('sntp@hotmail.com', 3), ('Jan', 27), ('15:46:24', 1), ('14:50:18',
... 1),  ('11:37:30', 1), ('18:10:48', 1), ('17:07:00', 1), ('09:05:31',
... 1),  ('10:38:42', 1), ('sdusa@yahoo.com', 1),
... ('"Sammy Davis, Jr." <samuel.david@gmail.com>', 4) ])
>>> 
>>> from email.utils import parseaddr
>>> [parseaddr(s)[1] for s in d.keys() if '@' in parseaddr(s)[1]]
['sntp@hotmail.com', 'samuel.david@gmail.com', 'sdusa@yahoo.com']

Note: parseaddr Only separates the parts of a valid but complete RFC822 address; it does not validate that it is a real address.

dawg
  • 98,345
  • 23
  • 131
  • 206
  • Brilliant, short and simple. It worked. I love this community of developers and trust their expertise. It was a brain freeze and just couldn't think. You saved my lot of time. I hope to stay in touch for professional advice. A million thanks. This was also an alternate solution and it worked fine as well. I learned new things. – Samuel Mar 29 '15 at 02:52