Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update the-filter-built-in-function.md #2617

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ Types of change:

### Fixed

## May 4th 2021

### Changed
- [Python/Functional Programming - Arrays II - Modify Revision Question](https://github.com/enkidevs/curriculum/pull/2617)

## April 27th 2021

### Added
Expand Down Expand Up @@ -252,6 +257,7 @@ Types of change:
- [TypeScript - Introduction to Type Aliases - Remove extra question gap](https://github.com/enkidevs/curriculum/pull/2624)



## February 5th 2021

### Fixed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Together with the other built-in functions `map`[1] and `reduce`, `filter` allow
filter(function, iterable)
```

`filter` always returns a list, *unless the iterable object passed in is a string or a tuple*; in this case, the return type reflects the input type. If we don't provide a function to `filter`, such as by putting `None` in the place of the function, `filter` assumes the identity function.
`filter` returns an iterable `filter object`, an iterable can be a tuple, a list, and so on. If we don't provide a function to `filter`, such as by putting `None` in the place of the function, `filter` assumes the identity function.

It is important to remember that `filter` evaluates a boolean value, so it interprets the results of the identity function as such. Therefore—*since Python evaluates `0`, `None` and the empty string as `False`*—a `filter` with `None` as its function will not return these objects if they occur in the iterable object.

Expand Down Expand Up @@ -104,15 +104,24 @@ What is the printed result of the following code execution?
numbers = [-3, -2, -1, 0, 1, 2, 3]
def mystery_function(element):
return element < 0
print(filter(mystery_function, numbers))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could have just converted the filter object to list,

filtered = filter(mystery_function, numbers)
print(list(filtered))

I think, this approach is straightforward instead of the for loop..


filtered = filter(mystery_function, numbers)
print(list(filtered))

# ???
# ???
# ???
```

???

- `[-3, -2, -1]`
- `[-1, -2, -3]`
- `[1, 2, 3]`

- `-3`
- `-2`
- `-1`
- `3`
- `2`
- `1`
- `0`

---

Expand Down