Nest all content into snippets

This commit is contained in:
Angelos Chalaris
2023-05-07 16:07:29 +03:00
parent 2ecadbada9
commit 6a45d2ec07
1240 changed files with 0 additions and 0 deletions

View File

@ -0,0 +1,24 @@
---
title: Map list to dictionary
type: snippet
language: python
tags: [list,dictionary]
excerpt: Maps the values of a list to a dictionary using a function.
cover: colors-mural
dateModified: 2020-11-02T19:28:27+02:00
---
Maps the values of a list to a dictionary using a function, where the key-value pairs consist of the original value as the key and the result of the function as the value.
- Use `map()` to apply `fn` to each value of the list.
- Use `zip()` to pair original values to the values produced by `fn`.
- Use `dict()` to return an appropriate dictionary.
```py
def map_dictionary(itr, fn):
return dict(zip(itr, map(fn, itr)))
```
```py
map_dictionary([1, 2, 3], lambda x: x * x) # { 1: 1, 2: 4, 3: 9 }
```