You are on page 1of 12

9/22/21, 11:55 PM 03 List Less Than Ten Solutions

PRACTICE PYTHON
Beginner Python exercises Follow @practice_python

Home
Why Practice Python?
Why Chilis?
Resources for learners
Exercises
Blog
About

26 FEBRUARY 2014

List Less Than Ten Solutions

Exercise 3
Take a list, say for example this one:

a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]

and write a program that prints out all the elements of the list that are less than 5.

Extras:

1. Instead of printing the elements one by one, make a new list that has all the elements less than 5
from this list in it and print out this new list.
2. Write this in one line of Python.
3. Ask the user for a number and return a list that contains only elements from the original list a that
are smaller than that number given by the user.

Sample solution
I will note that none of the solutions that were submitted were written in one line of Python. There
will be more exercises later that show you how to do this!

Here is a sample solution that solves the exercise, including extras 1 and 3.

Share the fun!

« Previous exercise Next exercise »

https://www.practicepython.org/solution/2014/02/26/03-list-less-than-ten-solutions.html 1/12
9/22/21, 11:55 PM 03 List Less Than Ten Solutions

ALSO ON PRACTICEPYTHON

4 years ago • 287 comments 4 years ago • 190 comments 4 yea


08 Rock Paper
Scissors 18 Cows And 30
Solutions Bulls Solutions So

https://www.practicepython.org/solution/2014/02/26/03-list-less-than-ten-solutions.html 2/12
9/22/21, 11:55 PM 03 List Less Than Ten Solutions

Sponsored

Dog Breeds You Should Avoid Bringing Into Your Home At All Costs
StyleReads.com

Brad Pitt's Daughter Is Probably The Prettiest Girl Who Ever Existed
Weight loss groove

If you own a mouse, you have to try this strategy game. No Install.
Combat Siege

Travel Credit Cards of 2021 - find options


Credit Card Options | Search Ads

Woman Refuses To Take Bag Off Seat, Gets Taught Lesson


Wanderoam

Cloud Computing Might Be Easier Than You Think


Cloud Computing | Search Ads

Comments Community 🔒 Privacy Policy 


1 Login

 Recommend 28 t Tweet f Share Sort by Oldest

Join the discussion…

LOG IN WITH
OR SIGN UP WITH DISQUS ?

Name

Sachin Patole • 3 years ago


print([aa for aa in a if aa < 5])
43 △ ▽ 9 • Reply • Share ›

Jash > Sachin Patole • 3 years ago


Can you please explain? New in Python
△ ▽ 2 • Reply • Share ›

Heii > Jash • 3 years ago • edited


@Jash

A list comprehension behaves like this:


[ ] f [i ] i [li ] if [fil ]
https://www.practicepython.org/solution/2014/02/26/03-list-less-than-ten-solutions.html 3/12
9/22/21, 11:55 PM 03 List Less Than Ten Solutions
[output] for [item] in [list] if [filter]

As you can see there are 4 components in its


syntax:

output, item, list and filter.

In the case of Sachin's code [aa for aa in a if


aa < 5]:

output = aa

item = aa

list = a

filter = aa < 5

What this means is that I'm outputting the


variable 'aa' which refers to each item in the
li ( )
see more

23 △ ▽ 1 • Reply • Share ›

Siddharth Mulampalli > Heii


• 3 years ago • edited
how about the same when we have
string in the list?
△ ▽ • Reply • Share ›

R3X > Siddharth Mulampalli


• 2 years ago • edited
Here is one example! :)

fruits = ["banana", "orange",


"pinapple", "banana"]

print([object for object in fruits if


object == "banana"])

This would return "banana" 2 times,


since there is a total of two objects
called "banana" in the list "fruits".
9△ ▽ • Reply • Share ›

Atelier reper > R3X


• 4 months ago
Thank you :-)
△ ▽ • Reply • Share ›

Dyogo Lorenz > Siddharth


Mulampalli • a year ago
int(str)
△ ▽ • Reply • Share ›

R3X > Heii • 2 years ago


Brilliant solution!
1△ ▽ • Reply • Share ›

SharisM > Heii • 2 years ago


This is very cool! Thank you :)
https://www.practicepython.org/solution/2014/02/26/03-list-less-than-ten-solutions.html 4/12
9/22/21, 11:55 PM 03 List Less Than Ten Solutions

△ ▽ • Reply • Share ›

M'hamed Chehbouni > Heii


• 2 years ago
its not really working
△ ▽ • Reply • Share ›

Michael Fantacone > M'hamed


Chehbouni • 2 years ago
a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]

print([num for num in a if num < 5])


5△ ▽ 2 • Reply • Share ›

Yukio Kirk O'Sullivan > Heii


• 21 days ago
Amazing. Thank you!!
△ ▽ • Reply • Share ›

Aymen Chehaider > Jash • 3 years ago


that's FKING Awsome.
2△ ▽ • Reply • Share ›

Jensen Ko > Aymen Chehaider


• 3 years ago
I never thought that code will be so
simple.
3△ ▽ • Reply • Share ›

S N > Jash • a year ago


So, I'm new to python too. I solved the same
thing with a for loop and an if statement
within it, and I realize that this is the same
as that but all in one line in the same order.
Also, the output is given as a list.

-------------------------------------------

I wrote:

a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]

for numbers in a:

if numbers <5:

print(numbers)

So, copy paste the lines above in this order to


get what Sachin has with minor
modification: "line 4" + "line 2" + "line 3"

print([numbers for numbers in a if numbers


<5])
see more

1△ ▽ • Reply • Share ›

Don > Sachin Patole • 3 years ago


yeah i'm curious too, very cool though.
△ ▽ • Reply • Share ›

https://www.practicepython.org/solution/2014/02/26/03-list-less-than-ten-solutions.html 5/12
9/22/21, 11:55 PM 03 List Less Than Ten Solutions

Sachin Patole > Don • 3 years ago


That's list compreshension...
△ ▽ 1 • Reply • Share ›

Kriss > Sachin Patole • 2 years ago • edited


this makes SO much more sense that the "oh-look-i-
am-so witty" solution they gave above
1△ ▽ • Reply • Share ›

Clay Kami > Sachin Patole • 2 years ago


I wrote it:

a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]

for nums in a:

if nums < 5:

print([nums])

but the way you condensed it...that's insane


4△ ▽ 1 • Reply • Share ›

Masoom Ali > Clay Kami • 2 years ago


does it works?
1△ ▽ • Reply • Share ›

Non-Descript Virt > Masoom Ali


• 2 years ago
I think just by looking but
poster/professor-role says he's
looking for one liner which the above
did in list comprehension which is
python-unique I think, but is saying
the same thing in one line.
△ ▽ • Reply • Share ›

Paulo Moore > Sachin Patole • 2 years ago • edited


I got an error with that code but I fixed it by
changing all the "aa" to "a":

print([a for a in a if a < 5])

Maybe python changed that in the new versions, or


maybe it is because I use windows haha
△ ▽ • Reply • Share ›

Rahul Roy • 3 years ago


print("Welcome to the List Less Than Ten program..")

print("\nEnter number as a list\n")

a=[int(i) for i in input().split()]

b=[int(j) for j in a if j<5]

print(b)
1△ ▽ 1 • Reply • Share ›

PoPcornfillinG > Rahul Roy • 3 years ago


rahul whats does \n \n mean. The one you put
https://www.practicepython.org/solution/2014/02/26/03-list-less-than-ten-solutions.html 6/12
9/22/21, 11:55 PM 03 List Less Than Ten Solutions

in your line two


△ ▽ • Reply • Share ›

Evan Krueger > PoPcornfillinG


• 3 years ago
\n creates a new line in the output

example: in: print('Hello\nWorld')

out: Hello

World
△ ▽ • Reply • Share ›

RA > Rahul Roy • 3 years ago


How should the entry of the list be

[1,9,2,3]?
△ ▽ 1 • Reply • Share ›

Subrat Kumar • 3 years ago


a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]

x=a

for x in a:

if x <= 5:

print(f'this value of this index is less than 5 and it is {x}')

else:

pass
5△ ▽ 4 • Reply • Share ›

nitin chaudhary > Subrat Kumar • 3 years ago


a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]

x=a

for x in a:

(i ran this code,Showing syntax error, at if ,)if x <=


5:

print(f'this value of this index is less than 5 and it is


{x}')

else:

pass
△ ▽ 1 • Reply • Share ›

Prasanna Kumar Kosanapalli > nitin


chaudhary • 3 years ago
its working
△ ▽ • Reply • Share ›

Balaji > nitin chaudhary


• 3 years ago • edited
Try to follow the Indentation. In python
Indentation must be follow. Because there
no Curly Braces. So that we should follow
Indentation. Better You Keep following the
indentation in all programming language it
is a good Practicing manner.
△ ▽ • Reply • Share ›

Sandeep Tripathi > Subrat Kumar • 3 years ago


https://www.practicepython.org/solution/2014/02/26/03-list-less-than-ten-solutions.html 7/12
9/22/21, 11:55 PM 03 List Less Than Ten Solutions

Your code shows errors..


△ ▽ 1 • Reply • Share ›

Ahmed Al-Othman > Sandeep Tripathi


• 3 years ago
Yes it's
△ ▽ 1 • Reply • Share ›

Balaji > Ahmed Al-Othman


• 3 years ago
Try to follow the Indentation. In
python Indentation must be follow.
Because there no Curly Braces. So
that we should follow Indentation.
Better You Keep following the
indentation in all programming
language it is a good Practicing
manner. Try To Follow this
△ ▽ • Reply • Share ›

archana > Subrat Kumar • 2 years ago


what is the meaning of this print( f ) i am not
understanding why be are used f.
3△ ▽ • Reply • Share ›

copavi_gubavac > archana • 2 years ago


it is print formatting, a slightly faster way to
print vars without concatenation.

on the other hand it just makes vars in print


statements look exactly like rest of strings so
it is a little less readable if you are going fast
through code looking for vars...
△ ▽ • Reply • Share ›

PoPcornfillinG • 3 years ago


a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]

num = int(input("Choose a number: "))

new_list = []

for i in a:

if i < num:

new_list.append(i)

print (new_list)
8△ ▽ 1 • Reply • Share ›

Sandy • 3 years ago


a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]

# prints all elements in a which are less than 5

for i in a:

if i < 5:

print(i)
https://www.practicepython.org/solution/2014/02/26/03-list-less-than-ten-solutions.html 8/12
9/22/21, 11:55 PM 03 List Less Than Ten Solutions

# Above code in one line


print([i for i in a if i < 5])

# creates a new list a_new with all the numbers less than 5
from list a

a_new = []

for i in a:

if i < 5:

a_new.append(i)

print(a_new)

# Ask the user for a number and return a list that contains
only elements

# from the original list a that are smaller than that number
given by the user.

num = int(input("Please enter a number: "))

for i in a:

if i < num:

print(i)
2△ ▽ • Reply • Share ›

Petar Matev > Sandy • 2 years ago


number3 = int(input("Give me a number: "))

number_list5 = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]

print([x for x in number_list5 if x < number3])


△ ▽ • Reply • Share ›

Jonathan > Sandy • a year ago


a = [2,55,47,2,8,4,65,9,123,53,75]

print([i for i in a if i < 5])

This is my own code and it works by adding the


brackets inside the parenthesis.
△ ▽ • Reply • Share ›

Thrive • 3 years ago


erm.. guys, if you are following the ans and is new like me,
and using py 3.6

you need change raw_input to input

and add the print( ) to new_list ..

so its:

--------------------

num = int(input("Choose a number: ")) <<<<<<< changed


here

print(new_list) <<< changed here

-------------
https://www.practicepython.org/solution/2014/02/26/03-list-less-than-ten-solutions.html 9/12
9/22/21, 11:55 PM 03 List Less Than Ten Solutions

or hey, code it yourself and experiment, that is what i am


doing. this is fun !
△ ▽ • Reply • Share ›

James Corden • 3 years ago


a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]

num = int(input())

b = list(filter(lambda x: x < num, a))

print(b)
△ ▽ • Reply • Share ›

Ifecojustin • 3 years ago


while True:

try:

number_list =[int(number) for number in input("Put in


the list of number: ").split()]

limiter= int(input("please Input the Number for limiting:


"))

break

except ValueError:

print("This program only runs on list of Integers only: ")

for limit in number_list:

if limit < limiter:

limit_list.append(limit)

else:

continue

if len(limit_list) ==0:

print("There is no number less than {} in the list


provided".format(limiter))

elif len(limit_list) ==1:

print("The number less than {} in the list provided is


:".format(limiter), end=" ")

for new in limit_list:

print(new, end=' ')

else:

print("The numbers less than {} in the list provided are:


".format(limiter), end=" ")

for new in limit_list:

print(new, end=', ')


1△ ▽ • Reply • Share ›

Allen Varughese • 3 years ago


list = [10,2,3,4,5,7,1,2,12,4,3,45,6,4,2]

newlist=[]

for i in list:

if i<5:

print(i)

newlist.append(i)

print(newlist)
△ ▽ • Reply • Share ›

Jernej Šorn > Allen Varughese • 3 years ago

https://www.practicepython.org/solution/2014/02/26/03-list-less-than-ten-solutions.html 10/12
9/22/21, 11:55 PM 03 List Less Than Ten Solutions
the problem I have with running a code this way is
that a loop is taking out numbers >5 one by one so I
actually get 4 new lists:

[1]

[1, 1]

[1, 1, 2]

[1, 1, 2, 3]

How do you deal with that?


△ ▽ • Reply • Share ›

Heba Eltoukhy > Jernej Šorn • 3 years ago


I have the same problem. have you figured
out how to fix that?
△ ▽ • Reply • Share ›

Lou D. Martin > Heba Eltoukhy


• 3 years ago
remove the line where it says print(i)

This line is printed in each loop if not


Sponsored

Monkey Raised With Cats Has A Most Peculiar Behavior


YourDailySportFix

After Being Rescued By a Pack of Huskies, This Kitten Thinks She’s a Dog
TravelerMaster

Work From Home Jobs Might Actually Surprise You


Work From Home | Search Ads

Getting a Master Degree in the United Kingdom Might be Easier Than You Think
Master Degree in UK | Search Ads

30 Rare Pics Of Princess Diana You Probably Have Never Seen


Car Novels

Couple’s Life Was Forever Changed After They Accepted This 30-Day Challenge
EveryDayMonkey

A Mysterious Birth Leaving Everyone's Jaw Dropped

Copyright Michele Pratusevich, 2014-2017.

https://www.practicepython.org/solution/2014/02/26/03-list-less-than-ten-solutions.html 11/12
9/22/21, 11:55 PM 03 List Less Than Ten Solutions

Advertising disclosure

https://www.practicepython.org/solution/2014/02/26/03-list-less-than-ten-solutions.html 12/12

You might also like