Python Sort() method [duplicate] - python

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 months ago.
I am starting to learn Python.
Can someone explain why sort() returns None?
alist.sort() ## correct
alist = blist.sort() ## NO incorrect, sort() returns None
Why shouldn't
alist = blist.sort()
return the sorted list and give it back to alist? This does not make sense to me.
Thanks.

alist.sort() sorts alist in-place, modifying alist itself.
If you want a new list to assign somewhere, use blist = sorted(alist)
list.sort(): http://docs.python.org/library/stdtypes.html#mutable-sequence-types
sorted(): http://docs.python.org/library/functions.html#sorted

Use the following:
alist = sorted(blist)

When you want to perform the sorting on same list then you have to use sort() method of list. But If you dont want to change the sequence of original list but you need a sorted copy of the original list then use sorted() inbuilt function.

Related

Issue in Python lists [duplicate]

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.

Unable to edit list after converting from tuple [duplicate]

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.

Not able to return list [duplicate]

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.

Functions and variable assignment Python [duplicate]

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.

Python - Extending a list directly results in None, why? [duplicate]

This question already has answers here:
How do I concatenate two lists in Python?
(31 answers)
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.
x=[1,2,3]
x.extend('a')
Output:
x is [1,2,3,'a']
But when I do the following:
[1,2,3].extend('a')
Output:
None
Why does extend work on a list reference, but not on a list?
2nd Part:
I found this because I was trying to append a listB to a listA while trying to extend listC to listB.
listA.append([listB[15:18].extend(listC[3:12])])
Supposing lists cannot be directly appended / extending. What is the most popular work around form for resolving this issue?
list.extend modifies the list in place and returns nothing, thus resulting in None. In the second case, it's a temporary list that is being extended which disappears immediately after that line, while in the first case it can be referenced via x.
to append a listB to a listA while trying to extend listC to listB.
Instead of using extend, you might want to try this:
listA.append(listB[15:18] + listC[3:12])
Or do it in multiple simple lines with extend if you want to actually modify listB or listC.
extend will extend list it self. Return type of that method is None
If you want to union 2 list and add that list to another list then you have to use another way to add.
listB[15:18] = listC[3:12]
listA.extend(listB)

Categories