How to find odd numbers in python?

1. By using filter() method
a=[1,2,3,4,5,6,7,8,9,10]
odd=filter(lambda x:x%2!=0, a)
print (list(odd))
#Output:[1, 3, 5, 7, 9]

2.By using list comprehension

a=[1,2,3,4,5,6,7,8,9,10]
odd=[x for x in a if x%2!=0]
print (odd)
#Output:[1, 3, 5, 7, 9]

2 comments

Leave a Reply to Indhumathy Chelliah Cancel reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s