Member Avatar

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

Recommended Answers

All 2 Replies


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]
>>> 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
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.