
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Filter Tuples with All Even Elements in Python
The problem statement allows us to determine the position of the desired string in the list, which can be useful for various purposes, such as accessing or manipulating elements at that specific index. In Python, we have some built-in functions like reduce(), str(), filter(), lambda, and, all() that will be used to Find Index Containing String in List.
Syntax
The following syntax is used in the examples-
reduce()
This function is defined under the functools module
str()
This is a built-in method in Python that can convert the specified value into a string.
filter()
The filter() method is applied when we filter the items based on specific conditions. In simple terms, it allows users to iterate those elements that are extracted to satisfy the condition.
lambda
The function lambda offers a shortcut for declaring brief anonymous functions using the lambda keyword. The lambda functions behave when expressed with the def keyword.
all()
This is the built-in method in Python that accepts the iterable object such as lists, dictionaries, tuples, etc.
Using list Comprehension and all() method
The program uses the list comprehension method which defines the combination of loop and condition expression in one-liner and also includes the method all().
Example
In the following example, start the program with the input tuple list in the variable tuple_list. Then use the list comprehension technique which set the condition for an even number by using modulo % and == operator using the for loop and if-statement. Finally, we are printing the output with the help of the variable result.
tuple_list = [(61, 14, 12, 18), (52, 61, 73, 69), (80, 111, 211),(2,4)] result = [tup for tup in tuple_list if all(ele % 2 == 0 for ele in tup)] print("After Filteration of all the tuple:",result)
Output
After Filteration of all the tuple: [(2, 4)]
Using Recursion
The program uses the recursion that is defined by function calling to function itself. Inside the function, it will use the built-in method named len() and slicing to filter the tuples with all even elements.
Example
In the following example, the function getEvenValTupleRec() in the program defines a new tuple that has the elements at the even indices of the input tuple and returns a new tuple with those same items. By calling itself with a sliced version of the input tuple, the function uses recursion to achieve this. The program then create a tuple in the variable myTuple, displays it, and uses myTuple as a parameter to run the getEvenValTupleRec() function to return the even indexed elements. The program then prints the outcome.
def getEvenValTupleRec(myTuple): if len(myTuple) == 0 or len(myTuple) == 1: return () return (myTuple[0], ) + getEvenValTupleRec(myTuple[2:]) # Create tuple and print the tuple myTuple = (4, 1, 6, 8, 3, 7) print("The Given String:", str(myTuple)) # Access the even index in Tuple evenTuple = getEvenValTupleRec(myTuple) # Printing even indexed tuple print("All even indexed elements of the tuple:", str(evenTuple))
Output
The Given String: (4, 1, 6, 8, 3, 7) All even indexed elements of the tuple: (4, 6, 3)
Using filter() and lambda
The program uses the method named filter to remove the non-even elements tuples from the list and lambda to calculate the condition for an even number.
Required installation:
pip install functools
The above required command to install on the system that will help to run this problem statement.
Example
In the following example, start the program with a module named functools that provide a high-level built-in function that will filter the tuple with an even element. Then create the input string tuple list in the variable tuple_list. Then print the same to get the original list. Next, use the reduce() and lambda function to filter tuples with all even elements under the parameter of list() and store it in the variable result. Finally, we are printing the output with the help of variable result.
# import functools module import functools tuple_list = [(6, 4, 2, 8), (5, 6, 7, 6), (8, 0, 2), (7,)] # printing original list print("The original list is : " , str(tuple_list)) # Use reduce() and a lambda function result = list(filter(lambda t: functools.reduce( lambda x, y: x and (y % 2 == 0), t, True), tuple_list)) # print results print("Filtered Tuples : " , str(result))
Output
The original list is : [(6, 4, 2, 8), (5, 6, 7, 6), (8, 0, 2), (7,)] Filtered Tuples : [(6, 4, 2, 8), (8, 0, 2)]
Conclusion
We discussed the various ways to solve the problem statement. This type of method is useful because it allows us to do things like access or update individual elements based on their content, determine the occurrence or frequency of a specific string, or create conditional logic based on the position of the required string within the list.