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
- Python - Extract Key's Value, if Key Present in List and Dictionary
- Python | Sort tuple list by Nth element of tuple
- Python - Flatten tuple of List to tuple
- Python - Convert Tuple Matrix to Tuple List
- Python - Filter key's value from other key
- Python - Convert Tuple value list to List of tuples
- Python - Convert Key-Value list Dictionary to List of Lists
- Python - Convert list to Single Dictionary Key Value list
- Python - Sort dictionaries list by Key's Value list index
- Python - Convert List to key-value list by prefix grouping
- Python - Assigning Key values to list elements from Value list Dictionary
- Python - Convert String List to Key-Value List dictionary
- Python | Replace tuple according to Nth tuple element
- Python - Raise elements of tuple as power to another tuple
- Python - Convert Tuple String to Integer Tuple
- Python - Convert Tuple to Tuple Pair
- Python | Tuple key dictionary conversion
- Python | Remove Consecutive tuple according to key
- Python | Check if tuple exists as dictionary key
- Python | Test if key exists in tuple keys dictionary
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.