Typing list comprehensions (Py 3.9 and Py 3.10) #1048
Answered
by
dineshbvadhia
dineshbvadhia
asked this question in
Q&A
-
What is the correct way to type list comprehensions? For example: my_numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9] my_squares = [x + sqr(x) for x in my_numbers] |
Beta Was this translation helpful? Give feedback.
Answered by
dineshbvadhia
Jan 25, 2022
Replies: 2 comments 2 replies
-
The result of a list comprehension is simply a list. Often the type can be determined automatically by a type checker, but an explicit annotation is also possible: my_numbers: list[int] = [1, 2, 3, 4, 5, 6, 7, 8, 9]
my_squares: list[float] = [x + sqr(x) for x in my_numbers] This requires |
Beta Was this translation helpful? Give feedback.
1 reply
-
I was asking about the x in the list comprehension? |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
dineshbvadhia
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I was asking about the x in the list comprehension?