Professional Documents
Culture Documents
PRACTICE PYTHON
Beginner Python exercises Follow @practice_python
Home
Why Practice Python?
Why Chilis?
Resources for learners
Exercises
Blog
About
26 FEBRUARY 2014
Exercise 3
Take a list, say for example this one:
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.
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
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
LOG IN WITH
OR SIGN UP WITH DISQUS ?
Name
output = aa
item = aa
list = a
filter = aa < 5
23 △ ▽ 1 • Reply • Share ›
△ ▽ • Reply • Share ›
-------------------------------------------
I wrote:
for numbers in a:
if numbers <5:
print(numbers)
1△ ▽ • 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
for nums in a:
if nums < 5:
print([nums])
print(b)
1△ ▽ 1 • Reply • Share ›
out: Hello
World
△ ▽ • Reply • Share ›
[1,9,2,3]?
△ ▽ 1 • Reply • Share ›
x=a
for x in a:
if x <= 5:
else:
pass
5△ ▽ 4 • Reply • Share ›
x=a
for x in a:
else:
pass
△ ▽ 1 • Reply • Share ›
new_list = []
for i in a:
if i < num:
new_list.append(i)
print (new_list)
8△ ▽ 1 • Reply • Share ›
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
# 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.
for i in a:
if i < num:
print(i)
2△ ▽ • Reply • Share ›
so its:
--------------------
-------------
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
num = int(input())
print(b)
△ ▽ • Reply • Share ›
try:
break
except ValueError:
limit_list.append(limit)
else:
continue
if len(limit_list) ==0:
else:
newlist=[]
for i in list:
if i<5:
print(i)
newlist.append(i)
print(newlist)
△ ▽ • Reply • Share ›
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]
After Being Rescued By a Pack of Huskies, This Kitten Thinks She’s a Dog
TravelerMaster
Getting a Master Degree in the United Kingdom Might be Easier Than You Think
Master Degree in UK | Search Ads
Couple’s Life Was Forever Changed After They Accepted This 30-Day Challenge
EveryDayMonkey
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