1

I need to write a function of all_gt(nums, n) which nums is a list of number, n is a number, returns the list of numbers from nums that are greater than n. The order of elements should be preserved.

What I need to do is write in .append format and start by initializing the result to an empty list.

For example:

all_gt([1,2,3,4],4) => [] 
all_gt([2,3,4,5], 3) => [4,5]

This is what I have:

def all_gt(nums, n):
 for i in nums:
 if i > n:
 return nums.append(i)

I know what I did is wrong and I hope someone can help me solve this question.

Bhargav Rao
52.5k29 gold badges129 silver badges142 bronze badges
asked Mar 29, 2015 at 11:53
2
  • 1
    You are on the right track. First, create a new list in the method. Then append all the numbers i that are greater than n to that new list. After the for loop ran, return the new list. Commented Mar 29, 2015 at 11:57
  • 1
    There are 3 or 4 issues here. First, append returns None and modifies nums. Second, you need to construct a new list and this is absent from your code. Third, you need to return once all the required elements are added to the list, in your code you will return after one element is added to your list. Fourth you should look at idiomatic ways of iterating over lists in Python using list comprehensions, even if your assignment requires you to use append. Commented Mar 29, 2015 at 11:57

3 Answers 3

2

Your mistake

return nums.append(i) appends a value to the list in-place and returns None. A workaround can be

def all_gt(nums, n):
 num2 = []
 for i in nums:
 if i > n:
 num2.append(i)
 return num2

Better Ways

You can use a list comprehension

def all_gt(nums, n):
 return [i for i in nums if i > n]
answered Mar 29, 2015 at 11:54

3 Comments

The return num2 return nothing.
@icefish I have tested it. It works in my case. Just check if you have missed any part of my answer. Especially num2 = [] statement after the function definition
Oh my mistake, I still put a return infront of num2.append(i). It works now. Thank you so much!
2

You can use filter to keep only the elements greater than n:

>>> def all_gt(nums, n):
... return filter(lambda x: x > n, nums)
>>> all_gt([1, 2, 3, 4], 4)
[]
>>> all_gt([2, 3, 4, 5], 3)
[4, 5]
answered Mar 29, 2015 at 11:58

4 Comments

I love the use of functional programming part of python :) ,
Everyone secretly loves it and hates it
Yes I know but my assignment needs me to do in .append :(
Then Rao's answer should do
0

just applying list comprehension with a if condition will do the trick

nums = [ 1, 2, 3, 4, 5]
constraint = 3
filtered_result = [ x for x in nums if x > constraint]

will result into [4,5]

answered Mar 29, 2015 at 12:29

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.