954,549 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

help!

if i have a dictionary like this
{ric:24, ric1:46, ric2:23, mike:1, mike1:47}

and have a list that has 'ric' and 'mike'

how do i get all the values whose key contains 'ric' or 'mike'?

i want to ignore the last numbers

apeiron27
Newbie Poster
9 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

*untested

dic = {ric:24, ric1:46, ric2:23, mike:1, mike1:47}
values = [v for k, v in dic.items() if 'ric' in k or 'mike' in k]
ihatehippies
Junior Poster
190 posts since Oct 2008
Reputation Points: 33
Solved Threads: 13
 
>>> d = {'ric':24, 'ric1':46, 'ric2':23, 'mike':1, 'mike1':47}
>>> l = ['ric', 'mike']
>>> for name in l:
...     if name in d:
...         print '{0} has a value of {1}'.format(name, d[name])
...         
ric has a value of 24
mike has a value of 1
snippsat
Practically a Posting Shark
808 posts since Aug 2008
Reputation Points: 353
Solved Threads: 294
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You