Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

how to sort list python

a_list = [3,2,1]
a_list.sort()
Comment

how to sort a list in python

l=[1,3,2,5]
l= sorted(l)
print(l)
#output=[1, 2, 3, 5]
#or reverse the order:
l=[1,3,2,5]
l= sorted(l,reverse=True)
print(l)
#output=[5, 3, 2, 1]
Comment

Sorting a list using a named function

ex_lst = ['hi', 'how are you', 'bye', 'apple', 'zebra', 'dance']

def second_let(x):

    lst= []
    for wrd in x:
        lst.append(wrd[1])
    return lst

print(second_let(ex_lst))
print(sorted(second_let(ex_lst)))

sorted_by_second_let= sorted(ex_lst, key= second_let)
Comment

how to sort a list in python

old_list = [3,2,1]
old_list.sort()
Comment

python sort a list using defined order

>>> A = [[3,5],[1,3],[6,1]]
>>> B = [6,1,3]
>>> srt = {b: i for i, b in enumerate(B)}
>>> sorted(A, key=lambda x: srt[x[0]])
[[6, 1], [1, 3], [3, 5]]
Comment

python sort a list using defined order

>>> sorted(A, key = lambda i: B.index(i[0]))
[[6, 1], [1, 3], [3, 5]]
Comment

sort a list python

"""Sort in ascending and descending order"""
list_test = [2, 1, 5, 3, 4]

#ascending is by default for sort
#Time Complexity: O(nlogn)
list_test.sort()

#For descending order
#Time Complexity: O(nlogn)
list_test.sort(reverse=True)

#For user-define order
list_test.sort(key=..., reverse=...) 
Comment

PREVIOUS NEXT
Code Example
Python :: python create null matrix 
Python :: doctest python 
Python :: class __call__ method python 
Python :: how to avoid inserting duplicate records in orm django 
Python :: sanke in python 
Python :: How to perform topological sort of a group of jobs, in Python? 
Python :: create a dictionary from index and column pandas 
Python :: django template in views.py 
Python :: pyqt graph 
Python :: How to Access Items in a Set in Python 
Python :: python regex split 
Python :: armstrong number in python 
Python :: Encrypting a message in Python 
Python :: __str__ returned non-string (type User) 
Python :: how to len in the pythin 
Python :: virtual environment python 
Python :: pandas fillna multiple columns 
Python :: how to round whole numbers in python 
Python :: python split() source code 
Python :: pandas filter columns with IN 
Python :: django get admin url 
Python :: Excel file format cannot be determined, you must specify an engine manually 
Python :: replace by positions a string in a list with another string python 
Python :: python online compiler 
Python :: string slicing python 
Python :: import csv in python 
Python :: python convert np datetime to string 
Python :: opencv resize image 
Python :: how to add new column in django 
Python :: python and pdf 
ADD CONTENT
Topic
Content
Source link
Name
3+9 =