I want to print a key that contains a specific character in the dictionary.

Asked 2 years ago, Updated 2 years ago, 87 views

For example, I want to print only keys that contain 'a' characters in the dictionary named 'dics = { 'abdc' : 1234, 'abbbd' : 1244, 'adffbg' : 1255, 'bbbffgrg' = 1666}. How do I print them?

python dictionary

2022-09-21 09:58

1 Answers

>>> d = { 'abdc' : 1234, 'abbbd' : 1244, 'adffbg' : 1255, 'bbffgrg' : 1666}
>>> d.keys()
dict_keys(['abdc', 'abbbd', 'adffbg', 'bbffgrg'])
>>> [ k for k in d.keys() if 'a' in k ]
['abdc', 'abbbd', 'adffbg']


2022-09-21 09:58

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.