Change language

Python | Selective selection of values ​​in a list of tuples

Method # 1: Using dict () + get () + comprehension list
We can accomplish this particular task by first converting the list into a dictionary, and then applying a list comprehension to get the value of specific keys using the get function.

# Python3 demo code
# Selective selection value in a list of tuples
# using dict () + get () + comprehension list

 
# initialize the list of tuples

test_list = [( ’Nikhil’ , 1 ), ( ’ Akash’ , 2 ), ( ’Akshat ’ , 3 ), ( ’Manjeet’ , 4 )]

 
# picklist initialization

select_list = [ ’Nikhil’ , ’Akshat’ ]

 
# print original list

print ( "The original list is:" + str (test_list))

  
# print selection list

print ( "The selection list is:" + str (select_list))

 
# using dict () + get () + comprehension list
# Selective selection value in a list of tuples

temp = dict (test_list)

res = [temp.get (i, 0 ) for i in select_list]

 
# print result

print ( "The selective values ​​of keys:" + str (res))

Output:

The original list is: [(’Nikhil’, 1) , (’Akash’, 2), (’Akshat’, 3), (’Manjeet’, 4)]
The selection list is: [’Nikhil’, ’Akshat’]
The selective values of keys: [1, 3]

Method # 2: Using next () + list comprehension
This particular the problem can be solved with the following function, which iterates using iterators and therefore a more efficient way to achieve a possible solution.

# Python3 demo code
# Selective selection value in list of tuples
# using next () + list comprehension

 
# initializing the list of tuples

test_list = [( ’Nikhil’ , 1 ), ( ’ Akash’ , 2 ), ( ’Akshat’ , 3 ), ( ’ Manjeet’ , 4 )]

 < / code> 
# select list initialization

select_list = [ ’Nikhil’ , ’Akshat’ ]

  
# print original list

print ( "The original list is:" + str (test_list))

 
# print selection list

print ( "The selection list is:" + str (select_list))

  
# using next () + list comprehension
# Custom value selection in the list of tuples

res = [ next ((sub [ 1 ] for sub in test_list 

if sub [ 0 ] = = i), 0 ) for i in select_list]

 
# print result

print ( "The selective values ​​of keys:" + str (res))

Output:

The original list is: [(’Nikhil’, 1), (’Akash’, 2), ( ’Akshat’, 3), (’Manjeet’, 4)]
The selection list is: [’Nikhil’, ’Akshat’]
The selective values ​​of keys: [1, 3]

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News


Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method