This question already has answers here:
Python Sort() method [duplicate]
(3 answers)
Closed 7 years ago.
I am just learning Python and programming in general and have come across the following question. I cannot seem to find an answer/explanation to why the statement below passes None in the assignment. Just trying to understand. I am oversimplifying below and understand that once I call .sort() on before that before has changed as well.
before = [67,45,2,13,1,998]
after = before.sort()
print(after)
Why, when I print after is it None?
sort is an inplace operation, it does not return a value so as with all python functions that don't return a value returns None by default so you are setting after to None.
Just call.sort and the print your list:
before = [67,45,2,13,1,998]
before.sort() # inplace sorts original list
print(before)
Or use sorted if you want a new list:
before = [67,45,2,13,1,998]
after = sorted(before) # creates a completely new list
print(after)
Related
This question already has answers here:
Python Sort() method [duplicate]
(3 answers)
Closed 2 years ago.
Taking an intro to python course, the following code sorts the string words appropriately, but the function does not return sortString, so I'm not understanding why the output is correct. Can you please help me understand?
def sort_words(string):
splitString = string.split()
sortString = splitString.sort()
return splitString
print(sort_words('python is pretty cool'))
Python .sort() returns None, much like print() does. list.sort() works in place - meaning the list is sorted without needing to assign it to another variable name.
This question already has answers here:
How can I get a reversed copy of a list (avoid a separate statement when chaining a method after .reverse)?
(11 answers)
Closed 2 years ago.
Python Novice. Need to create a list which is a reverse of the previous list. Ran the follow code:
list_10 = [1,10,20,4,40,14]
list_11 = list_10.reverse()
print (list_11)
I get an output that says "None"
Any suggestions?
Because reverse() reverses the list in place, it returns none.
Try this.
list_10 = [1,10,20,4,40,14]
list_10.reverse()
list_11 = list_10
This question already has answers here:
How can I select a variable by (string) name?
(5 answers)
Closed 8 months ago.
names=['abcd','efgh']
nameoflist='names'
def(nameoflist=[]):
return nameoflist
I want to be able to return the entire list from the function
Assuming names is global as specified in the question, you can do this
names=['abcd','efgh']
nameoflist='names'
def return_names(nameoflist):
return globals()[nameoflist]
However, this is pretty ugly, and I'd probably try another way to do it. What do you need the name for? Is there any other way to get the information you're asking for?
This one way to do what you are asking. But it is not good programming.
names=['abcd','efgh']
def list_by_name(list_name):
return eval(list_name)
print(list_by_name('names'))
Also, argument list_name should be a string. It should not default to a list, which would make the function to fail if called without argument. It should not have a default value.
This question already has answers here:
Why do these list methods (append, sort, extend, remove, clear, reverse) return None rather than the resulting list?
(6 answers)
Closed 5 years ago.
I am a beginner to programming and was working in a for loop and realised the error I was getting is because of the reason in the title. Why Does the happen anyway? Is this a general property of mutable ojects?.
T
append modifies the list in place. There is no need to do L=L.append(x); Simply running L.append(x) should suffice. The reason L is being set to none in your code is because there is no return value for append
This question already has an answer here:
Proper Usage of list.append in Python
(1 answer)
Closed 7 years ago.
A=['1']
C=A.append('1')
print C
Why is the above code return None but not ['1', '1'] in Python ?
The reason why you are not getting anything back is because the append method has no return value.
You could do:
A=['1']
C=A
C.append('1')
print(C)
Then you should get the right answer for your case that you are expecting to get.
In Python, the append method mutates the list it was called on but does not return it. There's not much reason to either, since you already have a reference to the list.