⊗pyPmLsOLS 58 of 208 menu

Sorting Elements in a Source List in Python

The sort method is used to sort the elements in a list. If you leave its parameter empty, the elements will be sorted in ascending order. If you pass the value reverse=True, the elements will be sorted in descending order. It should be remembered that after using the sort method, the original list changes.

Let's sort the items in the list in ascending order:

lst = [3, 2, 1] lst.sort() print(lst) # [1, 2, 3]

Now let's arrange the elements in descending order:

lst = [1, 2, 3] lst.sort(reverse=True) print(lst) # [3, 2, 1]

Given a list:

lst = [4, 2, 5, 1, 3]

Sort it in ascending order.

Given a list:

lst = [4, 2, 5, 1, 3]

Sort it in descending order.

Given a list:

lst = [1, 2, 3, 4, 5]

Reverse the order of the list.

The lists are given:

lst1 = ['a', 'b', 'c'] lst2 = [3, 2, 1]

Write the code to get the following result:

[1, 2, 3, 'c', 'b', 'a']
byenru