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.
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 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 4 years ago.
I'm using the code below:
a = ('Monty Python', 'British', 1969) #a is a tuple
b=list(a) #this should convert it to a list if I'm not wrong
print(b) #the output till here is okay
c=b.append("abcd")
print(c) # the output for this is None
Can anyone explain why am I unable to edit after converting the tuple to a list??
.append() does not return a list.
You are doing c = b.append("abcd"), this makes no sense because b.append() does not return a list, it returns none.
Try print(type(b.append("abcd"))) and see what it prints. So as you can see python is working correctly.
Things like .append() .pop() do not return a new list, they change the list in memory.
This is called an inplace operation I believe
You're printing c whose job is to append. Print b instead, that's your list.
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.
I read in this tutorial that you could add two lists using either + or the .extend() method. Other than performance issues, they produce the same results.
In that case, if I want to return the first and last four items of a list, using slicing, why does the .extend() return None while the + operator returns the correct result for the following code:
# return first and last four items in list
def first_and_last_4(itr):
first_four = itr[:4]
print(first_four)
last_four = itr[-4:]
print(last_four)
# return first_four.extend(last_four)
return first_four + last_four
my_list = list(range(1,50))
print(first_and_last_4(my_list))
list.extend modifies the existing list. It does not return anything. list + list returns a new list.
In your example, after calling first_four.extend(last_four), you should return first_four.
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.