This question already has answers here:
Why do these list operations (methods: clear / extend / reverse / append / sort / remove) return None, rather than the resulting list?
(6 answers)
Closed 8 years ago.
a=[8,2,15,6,1]
a = a.sort()
print a
Why does it print None? Can you elaborate on all functions?
sort() and sorted() are different:
sort() sorts the list in-place and returns None.
sorted() creates a new list and returns it.
See here for further details.
Related
This question already has answers here:
Why do these list operations (methods: clear / extend / reverse / append / sort / remove) return None, rather than the resulting list?
(6 answers)
Closed 5 months ago.
I have a list M, containing 3 numpy arrays.
when I wrote M = M.reverse(), then M became the noneType.
Why this happens? To me the result should be a reversed list.
Thanks
Lists belong to the collection data types in Python and are implemented as objects with a set of collection methods, where not all methods return a value.
Specifically .reverse() is a method that directly modifies the list, but does not return a value, therefore to access the reversed list, you just use the variable it is assigned to (M in your case).
This question already has answers here:
Why do these list operations (methods: clear / extend / reverse / append / sort / remove) return None, rather than the resulting list?
(6 answers)
Closed 2 years ago.
I do not understand why this code does not return the list ordered:
A = {2:'a', 1: 'b', 3:'c'}
R = list(a.keys()).sort()
In fact it does not return anything. I know I could do it in other ways like sorted(a.keys).
Thanks
sort() does in-place changes to the list. What you can do is create a variable to store the keys and then sort it.
R = list(a.keys())
R.sort()
Use R = sorted(list(a.keys())) instead, as sort() serve as an in-place function
This question already has answers here:
Why do these list operations (methods: clear / extend / reverse / append / sort / remove) return None, rather than the resulting list?
(6 answers)
Closed 3 years ago.
Here is my code:
list_b=[1,2,3,4,5,6,7,8,9,10]
print (list_b)
if(1 in list_b):
t=list_b.append('hello')
print(t)
else:
t1=list_b.append(100)
print(t1)
In the console it is showing me None. (Image)
append() method does not return any value but updates existing list.
if you want to see the updated list, use print(list_b).
print(t) or print(t1) will return None as they don't have any return values.
You haven't stated the issue, but I'm assuming it's this:
t=list_b.append('hello')
append() modifies the array, it doesn't return a new array.
This question already has answers here:
Why do these list operations (methods: clear / extend / reverse / append / sort / remove) return None, rather than the resulting list?
(6 answers)
Closed 7 years ago.
Is there any difference between these two ways of returning lists?
Initially the list is empty.
my_list = []
method 1:
my_list.append(1)
return my_list
method 2
return my_list.append(1)
Actually, the second method is returning an empty list for me. Please clarify why it is happening like this
When you type the following:
return my_list
You are returning a list object. When you type the following:
return my_list.append(something)
You are returning the result of that method call. In the case of .append() that method is void, so you are effectively returning nothing. If the method .append() appended the argument you pass to it and then returned the modified list itself then you could do it, but that isn't the case.
This question already has answers here:
Why do these list operations (methods: clear / extend / reverse / append / sort / remove) return None, rather than the resulting list?
(6 answers)
Closed 7 years ago.
If valst is a True list (containing elements), why does this work:
valst.append(seq)
id_seq_dict[id] = valst
But this does not work:
id_seq_dict[id] = valst.append(seq)
Is it because the append method returns nothing?
The append() method modifies the list and returns nothing (None) indeed.