Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Python List Comprehension Error: Unexpected Output

Soham1087

Banned
Joined
May 31, 2022
Messages
17
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Location
India
Activity points
185
I'm encountering an unexpected output while using list comprehension in Python. I'm trying to create a list of squared values for even numbers in a given range, but the result is not what I anticipated. Here's the code I'm using:

Python:
even_numbers = [x for x in range(10) if x % 2 == 0]
squared_values = [x**2 for x in even_numbers]

print(squared_values)

I expected the output to be [0, 4, 16, 36, 64], but instead, I'm getting [0, 4, 16]. It seems like the last even number (8) and its corresponding squared value (64) are missing.

Can someone help me understand why this is happening and how to correct my list comprehension code to get the desired output? Is there something I'm overlooking in my approach? Your insights would be greatly appreciated. Thank you!
 
sorry, but running that example in python 3.10 gets:

Python:
>>> even_numbers = [x for x in range(10) if x % 2 == 0]
>>> print(even_numbers)
[0, 2, 4, 6, 8]
>>> squared_values = [x**2 for x in even_numbers]
>>> print(squared_values)
[0, 4, 16, 36, 64]

you are probably doing something else? (or getting the data in another way?)
 

LaTeX Commands Quick-Menu:

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top