Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Extract all odd numbers that are greater than -10 and less than 10 (exclusive) from the array and print them as a 1-d array.

What I have tried:

Python
z = originalarray[originalarray % 2 != 0]
z = z[z > -10]
z = z[z < 10]

Instead of first finding the odd numbers, then checking for less than -10 and again checking for greater than 10, I was wondering if I can apply all three conditions in a single line of code - I have tried the where a function with AND operator but it hasn't worked for me.
Posted
Updated 9-Dec-20 3:12am
v2

1 solution

Looks like you're trying to create a list comprehension:
5. Data Structures — Python 3.9.1 documentation[^]
Python
z = [x for x in originalarray if x > -10 and x < 10 and x % 2 != 0]
 
Share this answer
 
Comments
CPallini 10-Dec-20 3:35am    
5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900