-3

Ben Bitdiddle is required to implement a function at_least_n which takes in a list of integers and an integer n, and returns the original list with all the integers smaller than n removed.

Example:

>>> lst = list(range(10))
>>> lst2 = at_least_n(lst, 5)
>>> lst2
[5, 6, 7, 8, 9]
>>> lst is lst2
True

Define a code at_least_n.

HELP??? I've been staring at this and trying almost everything but I can't get an answer :/

Elazar
  • 20,415
  • 4
  • 46
  • 67

2 Answers2

3

List comprehension is our friend here:

>>> lst = list(range(10))
>>> [x for x in lst if x >= 5]
[5, 6, 7, 8, 9]

I don't write the full function for you. You have to be able to do it by yourself (It's a function of one line...).

Maxime Lorant
  • 34,607
  • 19
  • 87
  • 97
0

Breaking this down, you need three things:

  • a function
  • a loop inside that function which loops through all the items in the list (which is provided by an argument)
  • inside the loop: check if the number is larger or equal to the second argument of the function.

I guess it would be easier to just give you the code, but that is not really helpful. The words here should help you figuring it out yourself.

Bjorn
  • 5,272
  • 1
  • 24
  • 35
  • For OP: If you're following this approach and you're using `list.remove` during the iteration, then please have a look at this thread first: [Loop “Forgets” to Remove Some Items](http://stackoverflow.com/questions/17299581/loop-forgets-to-remove-some-items) – Ashwini Chaudhary Feb 26 '14 at 17:36