Sometimes, while working with record data, we can have a problem in which we need to extract the key which has matching value of K from its value list. This kind of problem can occur in domains that are linked to data. Lets discuss certain ways in which this task can be performed.
Method #1 : Using List comprehension
This task can be performed using List comprehension. In this, we iterate through each records and test it’s value list for K. If found we return that key.
The original list is : [('Gfg', [1, 3, 4]), ('is', [5, 8, 10]), ('best', [11, 9, 2])] The required key of list values : ['Gfg']
Method #2 : Using
filter() + lambda
The combination of above functions can also be used to perform this task. In this, filter() is used to check for existance in list and extract the required key with help of lambda.
The original list is : [('Gfg', [1, 3, 4]), ('is', [5, 8, 10]), ('best', [11, 9, 2])] The required key of list values : Gfg
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.