Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple Python question: Why can't I assign a variable to a sorted list (in place)? [duplicate]

Tags:

python

y = [1, 3, 2, 4]
x = y.sort()
print(x)

x is None. How come this sort of syntax works in, for example, Javascript, and not Python?

like image 587
professional1234 Avatar asked Apr 12 '20 16:04

professional1234


People also ask

How do you assign a variable to a list in Python?

To initialize a list in Python assign one with square brackets, initialize with the list() function, create an empty list with multiplication, or use a list comprehension. The most common way to declare a list in Python is to use square brackets.

Can I use sorted on a list Python?

The sorted() function can accept three parameters: the iterable, the key, and reverse. In other words, the sort() method only works on lists, but the sorted() function can work on any iterable, such as lists, tuples, dictionaries, and others.

How do I make a sorted copy of a list in Python?

Python provides two ways to sort a list, the built-in list method list. sort() and the built-in function sorted() . Although both will sort the elements of a list, if used incorrectly they can produce unexpected or undesired results.

Does Python sorted work in-place?

Python has two sort functions; one function that sorts a list in-place and the other sort function “sorted” that creates a new sorted list. In this post, we will see examples of “in-place” sorting.

How do I sort a list in Python iterable?

If you want to take an iterable and return a new, sorted list of its items, use the sorted builtin function. sort () doesn't return any value while the sort () method just sorts the elements of a given list in a specific order - ascending or descending without returning any value. So problem is with answer = newList.sort () where answer is none.

What is the difference between sort() and Sort() methods in Python?

sort() doesn't return any value while the sort() method just sorts the elements of a given list in a specific order - ascending or descending without returning any value. So problem is with answer = newList.sort() where answer is none. Instead you can just do return newList.sort(). The syntax of the sort() method is:

How to assign the same value to multiple variables in Python?

This is possible due to the fact that the data types are dynamically typed in python. This is why P ython is known as a dynamically typed programming language. If you want to assign the same value to more than one variable then you can use the chained assignment: Integers, decimals, floats are supported. Longs are also supported.

What is the difference between list sort and in place sort?

list.sort sorts the list in place, i.e. it doesn't return a new list. Just write return sorted (newList) is shorter. Doesn't matter here since the variable is local, but in-place sort could change a shared variable in some cases.


1 Answers

sort (and reverse) change the list in-place.

If you want, you can use sorted:

x = sorted(y)

This is explained in the section Data Structures in the documentation.

like image 192
Laurent LAPORTE Avatar answered Sep 26 '22 01:09

Laurent LAPORTE