Table of Contents
Introduction
The task is to find and remove the K length tuple.
Program
Approach 1
ip_list = [ (2, ), (1, 6), (1, ), (3, 7)] print("The given list : "+str(ip_list)) K = 1 #Using filter()+lambda output = list(filter(lambda x : len(x) != K, ip_list)) print("Output list: "+str(output))
Output:
Approach 2
ip_list = [ (2, ), (1, 6), (1, ), (3, 7)] print("The given list : "+str(ip_list)) K = 1 #Using lis comprehension and len() output = [i for i in ip_list if len(i) != K] print("Output list: "+str(output))
Output:
Explanation
In our first approach, we have used filter function with lambda. The lambda function extracts the K length elements from the tuple and filter() function is used to filter the output.
The second approach is list comprehension. The elements having length not equal to K is extracted and stored to the output variable.
0 Comments