Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

how to sort tuples in list python

items =[
    ("product1",10),
    ("product2", 2),
    ("product3", 5)
]

def value(item):     #the function return only the numbers
    return item[1]
  
items.sort(key=value)  #don't call the function but passing it
print(items)


#OR by using Lamda Function

items.sort(key= lambda item: item[1])

# Output >>> [('product2', 2), ('product3', 5), ('product1', 10)]
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #sort #tuples #list #python
ADD COMMENT
Topic
Name
3+6 =