I tried this :
def append_size(lst):
size = len(lst)
return lst.append(size)
it returns NONE
Why?
I tried this :
def append_size(lst):
size = len(lst)
return lst.append(size)
it returns NONE
Why?
Because the list.append() method does not have a return value. We need to return the list after appending to it.
lst.append(...)
return lst
Thank you Roy for your answers. Just after my post I found the solution. So I could finish this path and it’s because of your help. It was challenging at the beginning but oh so rewarding at the end
How long have you been programming and with which language did you start?
Before joining CC I developed old-school webs for some local non-profits, learning as I went. HTML/CSS were my first languages, then a few years in I started to learn JS, followed up by PHP. I didn’t really get into learning in depth until I joined here. I learned Python and Ruby here, but have kept my focus on beginner and intermediate level problem solving, hence you see me here on the front line with the newbies.
Great experience ! Thank you for being with us, Roy (the newbies)
I had used print(sorted_list) instead of return sorted_list. Output showed the sorted list followed by NON. Can someone explain why I get NONE with the Print statement?
#Write your function here
def combine_sort(lst1, lst2):
unsorted = lst1 + lst2
sortedList = sorted(unsorted)
print (sortedList)
#Uncomment the line below when your function is done
print(combine_sort([4, 10, 2, 5], [-10, 2, 5, 10]))
Returns None
and the print outputs it as such.
Replace the print (sortedList)
with return sortedList
and let the print statement at the end handle the output. That way it will have a return value to print.
I don’t know why I get None for this code. Can anyone help?
def append_size (lst):
x = len(lst)
new_list = lst.append(x)
return new_list
The append method has no return so new_list
will be None
. Instead, append directly to lst
and do not assign it. The expression is for all intents, a statement.
lst.append(...)
return lst
I got it. Thank of helping!
Thanks for everyone’s help above, I also got it! I made mine a bit simpler though:
def append_size(lst):
lst.append(len(lst))
return lst
it is simpler, thanks!
Why does the following code return None?
def append_size(lst):
return lst.append(len(lst))
Because the append method does not have an explicit return so Python returns None
for it.
list.append(value)
is not an expression (which return expects) but a statement (which return is unable to send) so the return from append is sent back. Hence, None
.
Your function name says what the function does, and no return is implied. That means it is a function that performs an action, and ends at that. There is no real need of a return unless we wish to print the outcome. The data structure is changed, and the return is immaterial.
>>> def append_size(lst):
lst.append(len(lst))
return lst
>>> l = [1,2,3,4,5,6,7,8,9,0]
>>> m = append_size(l)
>>> id(l)
45391240L
>>> id(m)
45391240L
>>> l,m
([1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 10], [1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 10])
>>> l = [1,2,3,4,5,6,7,8,9,0]
>>> print (append_size(l))
[1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 10]
>>>
We’re still printing l
, the original object.
Bottom line, we cannot return 1st.append(…).
Can’t I store ‘appended list’ to a new list and return the new list?
Like below…
def append_size(lst):
new_lst = lst.append(len(lst))
return new_lst
print(append_size([23, 42, 108]))
It returns None when I proceed it…
Thanks,
the advantage of mutable data types (like lists) is that we can append items to the existing list rather then creating a new list. These mutations are more efficient then constructing new lists every time
of course there are other ways we can construct new lists, by adding two lists together for example:
new_lst = lst + [len(lst)]
Though the append
method of list
does add the specified item to the end of the list
on which it is called, its return
value is None
. Accordingly, this adds the value of len(lst)
to the end of lst
, then assigns None
to new_lst
:
new_lst = lst.append(len(lst))
Thank you Sir, much appreciated.