Help with list returning


#1

Hey, I'm working on a larger project and was hoping somebody could help me with my basic error.

I am trying to make a function that takes a list, works out the largest number in that list then simply returns 1 to that number. ie in form (1,2,3,4,5) if 5 is entered. Not (1,5) which I sometimes am getting.

def listcount(list):
    n=max(list)
    for l in range(0,n):
        return l

Here is my current code and I am sure I am making a basic mistake, would be great if anyone could help me


#2

My indents haven't shown up when ive posted but they are there!


#3

if we have the following list:

[1,2,3,4,5]

what exactly should your function return?


#4

Not exactly sure what you're trying to do here, but Python does have a len() function on lists...

Manually, should look like this:

for (i in list):
   counter = counter +1

#6

so if i do this:

def listcount(list):
    # function does something

print listcount([1,2,3,4,5])

what should be outputted by the function call? Its unclear what you are trying to achieve


#8

i see, then you can do:

def listcount(list):
    n=max(list)
    new_list = []
    for l in range(1,n+1):
        new_list.append(l)
    return new_list

or if you like list comprehension:

def listcount(list):
    return [i for i in range(1,max(list)+1)]