Open In App

Find an Element In a List of Tuples

Last Updated : 22 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In Python programming, it is often necessary to locate an element inside a list of tuples. Tuples are arranged collections, and locating a particular piece inside them requires knowledge of a variety of strategies. When data is kept as tuples inside a list, this procedure is essential for data retrieval and modification. We will look at the ideas and methods for effectively locating items in a list of tuples in this article.

Find An Element In A List Of Tuples

Below, we are explaining the example of Find An Element In A List Of Tuples in Python. those are as follows.

Find An Element In A List Of Tuples Using Linear Search

In this example, in the below code `linear_search` function iterates through a list of tuples, searching for the first tuple containing the specified target element. In this example, it searches for the tuple with ‘kiwi’ as its element within the `data_list`.

Python3




# Define the linear_search function
def linear_search(data, target):
    for tup in data:
        if target in tup:
            return tup
    return None
 
data_list = [(4, 'grape'), (5, 'kiwi'), (6, 'melon')]
 
#Call the linear_search function
result = linear_search(data_list, 5)
 
print("Linear Search Result:", result)


Output

Linear Search Result: (5, 'kiwi')

Find An Element In List Of Tuples Using Indexing and Slicing

In this example, in below code`slicing_search` function iterates through a list of tuples, searching for the first tuple containing the specified target element. In this example, it searches for the tuple with ‘plum’ as its element within the `data_list’ using Python indexing and Python Slicing .

Python3




# Define the slicing_search function
def slicing_search(data, target):
    for i in range(len(data)):
        if target in data[i]:
            return data[i]
    return None
 
data_list = [(7, 'pear'), (8, 'plum'), (9, 'peach')]
 
# Call the slicing_search function
result = slicing_search(data_list, 9)
 
print("Slicing Search Result:", result)


Output

Slicing Search Result: (9, 'peach')

Find An Element In A List Of Tuples Using Built-in Functions

In this example `filter_next_search` function uses the `filter` and `next` functions to find the first tuple containing the specified target element within a list of tuples. In this example, it searches for the tuple with ‘raspberry’ as its element within the `data_list`.

Python3




#Define the filter_next_search function
def filter_next_search(data, target):
    result = next(filter(lambda tup: target in tup, data), None)
    return result
 
data_list = [(10, 'blueberry'), (11, 'raspberry'), (12, 'blackberry')]
 
# Call the filter_next_search function
result = filter_next_search(data_list, 'raspberry')
 
print("Filter and Next Search Result:", result)


Output

Filter and Next Search Result: (11, 'raspberry')

Find An Element In A List Of Tuples Using List Comprehension

In this example , in below code `list_comprehension_search` function utilizes List comprehension to find the first tuple containing the specified target element within a list of tuples. In this example, it searches for the tuple with ‘lime’ as its element within the `data_list`.

Python3




# Define the list_comprehension_search function
def list_comprehension_search(data, target):
    result = [tup for tup in data if target in tup]
    return result[0] if result else None
 
data_list = [(13, 'lemon'), (14, 'lime'), (15, 'orange')]
 
#Call the list_comprehension_search function
result = list_comprehension_search(data_list, 'lime')
 
print("List Comprehension Search Result:", result)


Output

List Comprehension Search Result: (14, 'lime')


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads