Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

sort by tuple

sorted_by_second = sorted(data, key=lambda tup: tup[1])
Comment

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)]
Comment

sorting tuples

sorted_by_second = sorted(data, key=lambda tup: tup[1])
Comment

PREVIOUS NEXT
Code Example
Python :: drop first two rows pandas 
Python :: Display if the column(s) contain duplicates in the DataFrame 
Python :: append python 
Python :: python filter dictionary by keys 
Python :: create virtual environments python 
Python :: import file in parent directory python 
Python :: how to create a python server 
Python :: keras declare functional model 
Python :: import antigravity in python 
Python :: ignore pytest errors or warnings 
Python :: how to add phone number to django user model 
Python :: check type of django messages 
Python :: python program to switch first and second characters in a string 
Python :: pandas dataframe replace inf 
Python :: see attributes of object python 
Python :: throw error in python 
Python :: pandas copy data from a column to another 
Python :: python write text file on the next line 
Python :: time difference between timestamps python 
Python :: print(hello world) 
Python :: strp datetime 
Python :: uninstall python using powershell 
Python :: matplotlib animate 
Python :: Get Time from timestamp in python 
Python :: how to setup django ionos hostig 
Python :: python if condition assignment in one line 
Python :: django q objects 
Python :: openpyxl check if worksheet exists 
Python :: convert 2d string array to float python 
Python :: python loop back to start 
ADD CONTENT
Topic
Content
Source link
Name
2+3 =