Skip to content
Related Articles

Related Articles

Python | Convert list elements to bi-tuples

Improve Article
Save Article
  • Last Updated : 25 Nov, 2022
Improve Article
Save Article

Sometimes, while working with Python lists, we can have a subproblem in which we need to tie a particular element with each of the list elements. This task can have its utility in the web development domain while working with queries and filters. Let’s discuss ways to solve this problem.

 Method 1: Using zip() + repeat() This problem can be solved using zip() which can be used to attach to each list element with a decided number using repeat(). 

Python3




# Python3 code to demonstrate working of
# Convert list elements to bi-tuples
# using zip() + repeat()
from itertools import repeat
 
# initialize list
test_list = [5, 6, 7, 8, 4, 3]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initialize attach element
ele = 'gfg'
 
# Convert list elements to bi-tuples
# using zip() + repeat()
res = list(zip(test_list, repeat(ele)))
 
# printing result
print("Tuple list after attaching element : " + str(res))

Output

The original list is : [5, 6, 7, 8, 4, 3]
Tuple list after attaching element : [(5, 'gfg'), (6, 'gfg'), (7, 'gfg'), (8, 'gfg'), (4, 'gfg'), (3, 'gfg')]

  Method #2 : Using map() + lambda This is yet another way in which this task can be performed. In this, we just extend the logic of adding element to list element to all elements using map(), by feeding it with lambda function to perform computation tasks. 

Python3




# Python3 code to demonstrate working of
# Convert list elements to bi-tuples
# using map() + lambda
from itertools import repeat
 
# initialize list
test_list = [5, 6, 7, 8, 4, 3]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initialize attach element
ele = 'gfg'
 
# Convert list elements to bi-tuples
# using map() + lambda
res = list(map(lambda key: (key, ele), test_list))
 
# printing result
print("Tuple list after attaching element : " + str(res))

Output

The original list is : [5, 6, 7, 8, 4, 3]
Tuple list after attaching element : [(5, 'gfg'), (6, 'gfg'), (7, 'gfg'), (8, 'gfg'), (4, 'gfg'), (3, 'gfg')]

Method #3: Using append() method

Python3




# Python3 code to demonstrate working of
# Convert list elements to bi-tuples
 
# initialize list
test_list = [5, 6, 7, 8, 4, 3]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initialize attach element
ele = 'gfg'
res = []
# Convert list elements to bi-tuples
for i in test_list:
    res.append((i, ele))
# printing result
print("Tuple list after attaching element : " + str(res))

Output

The original list is : [5, 6, 7, 8, 4, 3]
Tuple list after attaching element : [(5, 'gfg'), (6, 'gfg'), (7, 'gfg'), (8, 'gfg'), (4, 'gfg'), (3, 'gfg')]

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!