Menu

gitpiper

find_keys python Code Snippet in 2024

dictionaryintermediate

Last Updated: 17 May 2024

Finds all keys in the provided dictionary that have the given value.

  • Use dictionary.items(), a generator and list() to return all keys that have a value equal to val.
def find_keys(dict, val): return list(key for key, value in dict.items() if value == val)
ages = { 'Peter': 10, 'Isabel': 11, 'Anna': 10, } find_keys(ages, 10) # [ 'Peter', 'Anna' ]

python snippet similar to find_keys For You in May 2024

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️

© 2024 GitPiper. All rights reserved

Rackpiper Technology Inc

Company

About UsBlogContact

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️