Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

how to sort a list of lists in python

# You can use a lambda function, but you'll have to specify the index of the sorting key.

A = [[100, 'Yes'], [40, 'Maybe'], [60, 'No']]
print("Sorted List A based on index 0: % s" % (sorted(A, key=lambda x:x[0])))
B = [[2, 'Dog'], [0, 'Bird'], [7, 'Cat']]
print("Sorted List A based on index 1: % s" % (sorted(B, key=lambda x:x[1])))

# Also, you can use .sort() if you want to sort just by the first index

A = [[55, 90], [45, 89], [90, 70]]
A.sort()
print("New sorted list A is % s" % (A))
A.sort(reverse=True)
print("New reverse sorted list A is % s" % (A))

# You can even change the key sort, if you want to sort by length for example:

A = [[5, 90, 'Hi', 66], [80, 99], [56, 32, 80]]
A.sort(key=len) # <-
print("New sorted list A is % s" % (A))
Comment

sort a list of array python

from operator import itemgetter
A = [[10, 8], [90, 2], [45, 6]]
print("Sorted List A based on index 0: % s" % (sorted(A, key=itemgetter(0))))
B = [[50, 'Yes'], [20, 'No'], [100, 'Maybe']]
print("Sorted List B based on index 1: % s" % (sorted(B, key=itemgetter(1))))

"""
Output:
Sorted List A based on index 0: [[10, 8], [45, 6], [90, 2]]
Sorted List B based on index 1: [[100, 'Maybe'], [20, 'No'], [50, 'Yes']]
"""
Comment

python sort list

# sort() will change the original list into a sorted list
vowels = ['e', 'a', 'u', 'o', 'i']
vowels.sort()
# Output:
# ['a', 'e', 'i', 'o', 'u']

# sorted() will sort the list and return it while keeping the original
sortedVowels = sorted(vowels)
# Output:
# ['a', 'e', 'i', 'o', 'u']
Comment

how to sort list python

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

how to sort a list of lists in python

from operator import itemgetter
A = [[10, 8], [90, 2], [45, 6]]
print("Sorted List A based on index 0: % s" % (sorted(A, key=itemgetter(0))))
B = [[50, 'Yes'], [20, 'No'], [100, 'Maybe']]
print("Sorted List B based on index 1: % s" % (sorted(B, key=itemgetter(1))))
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

python sort list

vowels = ['e', 'a', 'u', 'o', 'i']
vowels.sort()
Comment

python sort list

# example list, product name and prices
price_data = [['product 1', 320.0],
             ['product 2', 4387.0],
             ['product 3', 2491.0]]

# sort by price
print(sorted(price_data, key=lambda price: price[1]))
Comment

Python - Sort Lists

thislist = ["orange", "mango", "kiwi", "pineapple", "banana"]
thislist.sort()
print(thislist)
Comment

sort list python

>>> a = [5, 2, 3, 1, 4]
>>> a.sort()
>>> a
[1, 2, 3, 4, 5]
Comment

how to sort a list in python

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

sort list python

>>> L = ['abc', 'ABD', 'aBe']
>>> sorted(L, key=str.lower, reverse=True) # Sorting built-in
['aBe', 'ABD', 'abc']
>>> L = ['abc', 'ABD', 'aBe']
>>> sorted([x.lower() for x in L], reverse=True)
['abe', 'abd', 'abc']
Comment

python sort list

prime_numbers = [11, 3, 7, 5, 2]

# sort the list
prime_numbers.sort()
print(prime_numbers)

# Output: [2, 3, 5, 7, 11]
Comment

sort list in python

l = [64, 25, 12, 22, 11, 1,2,44,3,122, 23, 34]

for i in range(len(l)):
    for j in range(i + 1, len(l)):

        if l[i] > l[j]:
           l[i], l[j] = l[j], l[i]

print l
Comment

Python Sort List

prime_numbers = [11, 3, 7, 5, 2]

# sorting the list in ascending order
prime_numbers.sort()

print(prime_numbers)

# Output: [2, 3, 5, 7, 11]
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

sort list in python

data_list = [-5, -23, 5, 0, 23, -6, 23, 67]
new_list = []

while data_list:
    minimum = data_list[0]  # arbitrary number in list 
    for x in data_list: 
        if x < minimum:
            minimum = x
    new_list.append(minimum)
    data_list.remove(minimum)    

print new_list
Comment

python sort list

# Sort with an inner object
# Here it will sort with "book_no"
# [
#   {
#     "key": "book-key-1",
#     "book_no": 1,
#     "name": "My Book Name 1"
#   },
#   {
#     "key": "book-key-2",
#     "book_no": 2,
#     "name": "My Book Name 2"
#   }
# ]

def sortOnNumber(e):
  return e['book_no']

@app.get('/getBooks')
def getBooks():
  res = next(booksDb.fetch())
  res.sort(key=sortOnNumber)
  if res:
        return res
    
  raise HTTPException(404,"Not found")
Comment

sort lists in python

stuff_1 = [3, 4, 5, 2, 1]
stuff_1.sort()
print(stuff_1)      # Output: [1, 2, 3, 4, 5]
stuff_2 = ['bob', 'john', 'ann']
stuff_2.sort()
print(stuff_2)      # Output: ['ann', 'bob', 'john']

stuff_4 = ['book', 89, 5.3, True, [1, 2, 3], (4, 3, 2), {'dic': 1}]
# print(stuff_4.sort())
# This will give us an error
# TypeError: '<' not supported between instances of 'int' and 'str'
Comment

PREVIOUS NEXT
Code Example
Python :: python loop index and value 
Python :: python math functions 
Python :: Display an image over another image at a particular co-ordinates in openCV 
Python :: how to write a dataframe to s3 object in python 
Python :: how to check if a variable holds a class reference in python 
Python :: how can I corect word spelling by use of nltk? 
Python :: How to Add Elements To a Set using add() method in python 
Python :: free download django app for windows 10 
Python :: numpy copy a array vertical 
Python :: fast input python 
Python :: Install discord.ui on windows 
Python :: Python script for computing descriptive statistics 
Python :: iterate over a list python 
Python :: getting multiple of 5 python 
Python :: check if string match regex python 
Python :: import module python same directory 
Python :: ImportError: cannot import name include 
Python :: python conditions 
Python :: while loop in python 
Python :: python script that turns bluetooth on 
Python :: slicing of strings in python 
Python :: how to give float till 5 decimal places 
Python :: isolationforest estimators 
Python :: Flatten List in Python Using NumPy flat 
Python :: python print string and variable 
Python :: how to add value in array django 
Python :: CACHE_TYPE flask 
Python :: python keyboard input 
Python :: SUMOFPROD1 Solution 
Python :: find string in list and return index python 
ADD CONTENT
Topic
Content
Source link
Name
6+7 =