Skip to content

Commit

Permalink
add
Browse files Browse the repository at this point in the history
  • Loading branch information
szabgab committed Jan 1, 2025
1 parent 5d1c853 commit e8961d5
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
3 changes: 3 additions & 0 deletions python/examples/functional/filter_dictionary.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{'math': 100, 'biology': 53, 'chemistry': 62, 'art': 78, 'history': 20}
{'math': 100, 'chemistry': 62, 'art': 78}
{'biology': 53, 'history': 20}
18 changes: 18 additions & 0 deletions python/examples/functional/filter_dictionary.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
def main():
grades = {
"math": 100,
"biology": 53,
"chemistry": 62,
"art": 78,
"history": 20,
}
print(grades)

good_grades = {key: value for (key, value) in grades.items() if value >= 60}
print(good_grades)

bad_grades = {key: grades[key] for key in grades if grades[key] < 60}
print(bad_grades)


main()
9 changes: 9 additions & 0 deletions python/functional.md
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,15 @@ This can have only one iterable!
![](examples/functional/filter_map_one.py)
![](examples/functional/filter_map_one.out)

## filter a dictionary using dict comprehension
{id: filter-a-dictionary}

We have a dictionary, we would like to create another dictionary based on this one that contains only key-value pairs that meet a certain condition.

![](examples/functional/filter_dictionary.py)
![](examples/functional/filter_dictionary.out)


## Get indices of values
{id: get-indexes-of-values}

Expand Down

0 comments on commit e8961d5

Please sign in to comment.