Skip to content
Related Articles

Related Articles

Python – Trim tuples by K

View Discussion
Improve Article
Save Article
  • Last Updated : 12 Nov, 2020
View Discussion
Improve Article
Save Article

Given the Tuple list, trim each tuple by K.

Examples:

Input : test_list = [(5, 3, 2, 1, 4), (3, 4, 9, 2, 1), (9, 1, 2, 3, 5), (4, 8, 2, 1, 7)], K = 2 
Output : [(2,), (9,), (2,), (2,)] 
Explanation : 2 elements each from front and rear are removed.
 

Input : test_list = [(5, 3, 2, 1, 4), (3, 4, 9, 2, 1), (9, 1, 2, 3, 5), (4, 8, 2, 1, 7)], K = 1 
Output : [(3, 2, 1), (4, 9, 2), (1, 2, 3), (8, 2, 1)] 
Explanation : 1 element each from front and rear are removed. 

Method #1: Using loop + slicing 

In this, we omit front and rear K elements by using slicing, converting tuple to list, then reconversion to the tuple.

Python3




# Python3 code to demonstrate working of
# Trim tuples by K
# Using loop + slicing
  
# initializing list
test_list = [(5, 3, 2, 1, 4), (3, 4, 9, 2, 1),
             (9, 1, 2, 3, 5), (4, 8, 2, 1, 7)]
  
# printing original list
print("The original list is : " + str(test_list))
  
# initializing K
K = 2
  
res = []
for ele in test_list:
    N = len(ele)
  
    # triming elements
    res.append(tuple(list(ele)[K: N - K]))
  
# printing result
print("Converted Tuples : " + str(res))


Output:

The original list is : [(5, 3, 2, 1, 4), (3, 4, 9, 2, 1), (9, 1, 2, 3, 5), (4, 8, 2, 1, 7)]
Converted Tuples : [(2,), (9,), (2,), (2,)]

Method #2: Using list comprehension + slicing 

In this, we perform tasks in a similar way as the above method, difference being list comprehension is employed to perform the task in one-liner.

Python3




# Python3 code to demonstrate working of
# Trim tuples by K
# Using list comprehension + slicing
  
# initializing list
test_list = [(5, 3, 2, 1, 4), (3, 4, 9, 2, 1),
             (9, 1, 2, 3, 5), (4, 8, 2, 1, 7)]
  
# printing original list
print("The original list is : " + str(test_list))
  
# initializing K
K = 2
  
# one-liner approach to solve problem
res = [tuple(list(ele)[K: len(ele) - K]) for ele in test_list]
  
# printing result
print("Converted Tuples : " + str(res))


Output:

The original list is : [(5, 3, 2, 1, 4), (3, 4, 9, 2, 1), (9, 1, 2, 3, 5), (4, 8, 2, 1, 7)]
Converted Tuples : [(2,), (9,), (2,), (2,)]


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!