Home > database >  Subset dictionary for keys starting with string
Subset dictionary for keys starting with string

Time:02-24

How to get the mydict subset for keys starting with/containing 'two' string?

mydict = {'one,a': 1, 'one,b': 2, 'two,a': 3, 'two,b': 3}

{'two,a': 3, 'two,b': 3}

CodePudding user response:

With a dictionary comprehension.

subdict = {key: value for (key, value) in mydict.items() if 'two' in key}
  • Related