Python List Reverse Function [duplicate] - python

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

Related

This python string sorting works but it shouldn't. Should it? [duplicate]

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.

In Python 3, How do I get an element I pick from my list to the first place [duplicate]

This question already has answers here:
Move an item inside a list?
(8 answers)
Closed 3 years ago.
I want to write a function in Python 3, to change the order of the elements in my list. First of all, I have to put the element I selected at the top of the list.
I tried this:
def change_order(my_list,window):
selected = window['my choice']
my_list.remove (selected)
my_list.insert (0,selected)
return my_list
this is definitely working, but is there any useful and pythonic solution?
Thanks.
You can use the pop method
my_list.insert(0, my_list.pop(my_list.index(selected)))

In Python: When L is a list and I assign L=L.appened(x) (for some object x), why do I get a None object? [duplicate]

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

Python += with a list and a tuple [duplicate]

This question already has answers here:
Why does += behave unexpectedly on lists?
(9 answers)
Why can't I add a tuple to a list with the '+' operator in Python?
(3 answers)
Closed 7 years ago.
I saw someone wrote an interesting python line online, but couldn't understand why it works. So we can try the following lines in python interpreter:
s=[1]
s=s+(1,-1)
This will result in an error "TypeError: can only concatenate list (not "tuple") to list". But if done in another way:
s=[1]
s+=(1,-1)
will result in s = [1,1,-1]
So I used to thought x=x+y is equivalent to x+=y, can someone tell me how they are different and why the second way works? Thanks in advance.
Instead of += use list.extend:
s = [1]
s.extend((1,-1))

Why is object.sort() passing None to variable? [duplicate]

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)

Categories

Resources