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 element. This task can have it’s utility in web development domain while working with queries and filters. Let’s discuss a ways to solve this problem.
Method : 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 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)) |
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 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)) |
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’)]
Recommended Posts:
- Python - Convert Value list elements to List records
- Python | Convert case of elements in a list of strings
- Python | Convert column to separate elements in list of lists
- Python | Convert mixed data types tuple list to string list
- Python | Convert list of string into sorted list of integer
- Python | Convert list of numerical string to list of Integers
- Python Program to convert List of Integer to List of String
- Python | Convert string List to Nested Character List
- Python | Convert list of string to list of list
- Python | Convert list of tuples to list of list
- Python - Convert list of dictionaries to Dictionary Value list
- Python - Convert Key-Value list Dictionary to List of Lists
- Python | Convert string enclosed list to list
- Python - Convert Tuple value list to List of tuples
- Python | Convert 1D list to 2D list of variable length
- Python | Convert list of strings to list of tuples
- Python | Convert a string representation of list into list
- Python - Convert List of Dictionaries to List of Lists
- Python | Convert List of Dictionary to Tuple list
- Python | Convert list to indexed tuple list
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

