Skip to content
క
kagitha
/
quiz
/
python-comprehensions
Subscribe
Index
⌘K
JAVA NOTEBOOK · QUIZ 21 · BEGINNER
Python comprehensions vs Java streams
QUESTION 01 / 04
Which comprehension matches docs.stream().filter(d -> d.score() > 0.5).map(d -> d.text()).toList()?
A
[d.score > 0.5 for d in docs if d.text]
B
[d.text for d in docs if d.score > 0.5]
C
[for d in docs: d.text if d.score > 0.5]
D
{d.text: d for d in docs}
SCORE SO FAR · 0 / 0
Share this quiz ↗
Which comprehension matches docs.stream().filter(d -> d.score() > 0.5).map(d -> d.text()).toList()?
— [d.text for d in docs if d.score > 0.5]. The front is what you keep (the map), the for is the loop, and the if is the filter. Read it: for, then if, then the front.
What does {tool.name: tool for tool in tools} build?
— A dict from name to tool. Braces with key: value at the front make a dict comprehension, like Collectors.toMap. Duplicate keys keep the last value.
What is the Python version of .anyMatch(d -> d.score() > 0.9)?
— any(d.score > 0.9 for d in docs). any() takes a generator expression and stops at the first true value, just like anyMatch. all() is allMatch.
Why does itertools.groupby surprise Java developers used to groupingBy?
— It only groups consecutive items, so the input must be sorted by the key first. groupby starts a new group every time the key changes. For a groupingBy-style result, sort first or use a defaultdict(list) loop.
kagitha
Close
✕
Search everything — articles, videos, chapters, agents, books
01
AI
02
Java
03
System Design
04
Series
05
Manga
06
The Lab
07
Videos
08
Quizzes
09
Books
10
Resume