Find an Element In a List of Tuples
Last Updated :
22 Jan, 2024
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)
OutputLinear 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)
OutputSlicing 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)
OutputFilter 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)
OutputList Comprehension Search Result: (14, 'lime')
Similar Reads
Python - Find first element by second in tuple List Sometimes, while working with Python records, we can have a problem in which we need to find the first element of tuple from the given second element. This kind of problem can occur in domains such as web development. Let's discuss certain ways in which this task can be performed. Input : test_list
4 min read
How toGet First and Last Elements from a Tuple in Python Tuples are immutable sequences in Python that can store a collection of items. Often, we might need to the retrieve the first and last elements from the tuple. In this article, we'll explore how to achieve this using the various methods in Python. Get First and Last Elements from a Tuple Using Index
2 min read
Find the Tuples Containing the Given Element from a List of Tuples - Python The task of finding tuples containing a given element from a list of tuples in Python involves searching for a specific target value within each tuple. For example, given a list like [(1, 2, 3), (4, 5, 6)] and a target 2, the goal is to extract tuples containing 2, resulting in [(1, 2, 3)].Using in
3 min read
Python | Count occurrences of an element in a Tuple In this program, we need to accept a tuple and then find the number of times an item is present in the tuple. This can be done in various ways, but in this article, we will see how this can be done using a simple approach and how inbuilt functions can be used to solve this problem. Examples: Tuple:
3 min read
Python - Count the elements in a list until an element is a Tuple The problem involves a list of elements, we need to count how many elements appear before the first occurrence of a tuple. If no tuple is found in the list, return the total number of elements in the list. To solve this, initialize a counter and use a while loop to iterate through the list, checking
2 min read