Guys, I am trying to sort a list. But Python does not let me set my new sorted list into a variable. here:

list1 = ['a', 'z', 'b', 'y']
sorted_list = list1.sort()
return sorted_list

When I run this on my interpreter, instead of printing my string sorted, it prints "None"
Why does it do that?

But when I go:

list1 = ['a', 'z', 'b', 'y']
list1.sort()
print list1

it returns my string sorted. But why does it return "None" when I assign it to a variable?

Use sorted_list = sorted(list1)

... Or from second form replace print with return

return list1

Thanks allot guys

.sort sorts the list, returns none. sorted() returns a sorted list/object, but doesn't do anything to the list.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.