pick

New in Atfinity 17.

Description

pick returns a new dictionary containing only the selected keys of the dictionary it is called on. The argument has the same shape as the argument to to_dict: a dictionary literal whose keys are the names to pick from the source.

For each entry:

  • If only a key is given (using the dictionary shorthand, e.g. first_name), the resulting dictionary uses the same key name.

  • If a value is given (e.g. first_name: 'givenName'), that value is used as the key in the resulting dictionary (i.e. the key is renamed).

Keys that are not present in the source dictionary are skipped and do not appear in the result.

Examples

{'first_name': 'Thorben', 'last_name': 'Croisé', 'age': 42}.pick({first_name, last_name})
// => {'first_name': 'Thorben', 'last_name': 'Croisé'}

Renaming all picked keys:

some_dict.pick({first_name: 'givenName', last_name: 'familyName'})
// => {'givenName': '...', 'familyName': '...'}

Mixing shorthand and explicit renames:

some_dict.pick({first_name, last_name: 'familyName'})

Last updated

Was this helpful?