Python | Associating a single value with all list items
Sometimes we come across a utility in which we have a list and we wish to associate with it any one of the given value. This can occur in many phases of programming and knowing the shorthands to it can be useful. Let’s discuss certain ways in which this can be done.
Method #1 : Using map() + lambda
This task can be done using map function which is inbuilt python function that is generally used to associate or aggregate values. Lambda function can feed a particular value to the map function for its execution.
# Python3 code to demonstrate # associate value in list # using map() + lambda # initializing list test_list = [1, 4, 5, 8, 3, 10] # initializing value to associate val = 'geeks' # printing the original list print ("The original list is : " + str(test_list)) # printing value print ("The value to be attached to each value : " + str(val)) # using map() + lambda # associate value in list res = list(map(lambda i: (i, val), test_list)) # printing result print ("The modified attached list is : " + str(res)) |
The original list is : [1, 4, 5, 8, 3, 10]
The value to be attached to each value : geeks
The modified attached list is : [(1, ‘geeks’), (4, ‘geeks’), (5, ‘geeks’), (8, ‘geeks’), (3, ‘geeks’), (10, ‘geeks’)]
Method #2 : Using zip() + itertools.repeat()
The zip function can be used to attach the required value with the elements in a sequence and repeat function can be used to extend the task to all the list elements in more efficient manner.
# Python3 code to demonstrate # associate value in list # using zip() + itertools.repeat() from itertools import repeat # initializing list test_list = [1, 4, 5, 8, 3, 10] # initializing value to associate val = 'geeks' # printing the original list print ("The original list is : " + str(test_list)) # printing value print ("The value to be attached to each value : " + str(val)) # using zip() + itertools.repeat() # associate value in list res = list(zip(test_list, repeat(val))) # printing result print ("The modified attached list is : " + str(res)) |
The original list is : [1, 4, 5, 8, 3, 10]
The value to be attached to each value : geeks
The modified attached list is : [(1, ‘geeks’), (4, ‘geeks’), (5, ‘geeks’), (8, ‘geeks’), (3, ‘geeks’), (10, ‘geeks’)]
Recommended Posts:
- Python | Count number of items in a dictionary value that is a list
- Python | Convert an array to an ordinary list with the same items
- Python | Insert the string at the beginning of all items in a list
- Python | Split a list having single integer
- Python | Convert a list of multiple integers into a single integer
- Python | Get first K items in dictionary
- Python | Remove items from Set
- Python | Common items among dictionaries
- Dictionary Methods in Python | Set 1 (cmp(), len(), items()...)
- Python | Pandas Series.items()
- Python Dictionary | items() method
- Python | Sort the items alphabetically from given dictionary
- Python | Type conversion of dictionary items
- Python | Delete items from dictionary while iterating
- Python | Get items in sorted order from given dictionary
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.



