Python | Associating a single value with all list items
Last Updated :
28 Jul, 2023
Sometimes we come across a utility in which we have a list and we wish to associate with it any one of the given values. 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.
Associating a single value with all list items Using map() + lambda
This task can be done using the map() function which is an inbuilt Python function that is generally used to associate or aggregate values. The lambda function can feed a particular value to the map function for its execution.
Python3
test_list = [ 1 , 4 , 5 , 8 , 3 , 10 ]
val = 'geeks'
print ( "The original list is : " + str (test_list))
print ( "The value to be attached to each value : " + str (val))
res = list ( map ( lambda i: (i, val), test_list))
print ( "The modified attached list is : " + str (res))
|
Output
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')]
Time complexity: O(n) where n is the length of the list “test_list”.
Auxiliary space: O(n) where n is the length of the list “res”.
Associating a single value with all list items 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
from itertools import repeat
test_list = [ 1 , 4 , 5 , 8 , 3 , 10 ]
val = 'geeks'
print ( "The original list is : " + str (test_list))
print ( "The value to be attached to each value : " + str (val))
res = list ( zip (test_list, repeat(val)))
print ( "The modified attached list is : " + str (res))
|
Output
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')]
Time Complexity: O(n) where n is the length of the list ‘test_list’.
Auxiliary Space: O(n) as we are creating a new list ‘res’ of length n.
Associating a single value with all list items Using for loop and tuple() method
Python3
test_list = [ 1 , 4 , 5 , 8 , 3 , 10 ]
val = 'geeks'
print ( "The original list is : " + str (test_list))
print ( "The value to be attached to each value : " + str (val))
res = []
for i in test_list:
x = [i, "geeks" ]
res.append( tuple (x))
print ( "The modified attached list is : " + str (res))
|
Output
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')]
The time complexity of this code is O(n), where n is the number of elements in the list “test_list”.
The auxiliary space complexity is O(n), as a new list “res” with n elements is created.
Python3
test_list = [ 1 , 4 , 5 , 8 , 3 , 10 ]
val = 'geeks'
res = list ( zip (test_list, (val for _ in test_list)))
print ( "The original list is :" , test_list)
print ( "The value to be attached to each value :" , val)
print ( "The modified attached list is :" , res)
|
Output
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')]
The zip() function combines the elements of two iterables into tuples, and the generator expression (val for _ in test_list) generates a sequence of vals that is as long as the list. The resulting tuples are then collected into a list using the list() function.
Time complexity: O(n), where n is the length of the list. This is because the zip() function and generator expression only need to iterate over the list once.
Auxiliary space: O(n), because the resulting list will have n elements.
Associating a single value with all list items Using list comprehension
Step-by-step approach:
- Initialize the list test_list and the value to associate val.
- Use a list comprehension to iterate over each element i in test_list and create a tuple (i, val).
- The resulting list of tuples is assigned to the variable res.
- Print the modified list res.
Below is the implementation of the above approach:
Python3
test_list = [ 1 , 4 , 5 , 8 , 3 , 10 ]
val = 'geeks'
res = [(i, val) for i in test_list]
print ( "The modified attached list is : " + str (res))
|
Output
The modified attached list is : [(1, 'geeks'), (4, 'geeks'), (5, 'geeks'), (8, 'geeks'), (3, 'geeks'), (10, 'geeks')]
Time Complexity: O(n) as we iterate through the list once.
Auxiliary Space: O(n) as we create a new list of tuples.
Associating a single value with all list items Using numpy library
Python3
import numpy as np
test_list = [ 1 , 4 , 5 , 8 , 3 , 10 ]
val = 'geeks'
arr = np.array(test_list)
str_arr = np.full(( len (test_list),), val)
res_arr = np.column_stack((arr, str_arr))
res_list = res_arr.tolist()
print ( "The modified attached list is : " + str (res_list))
|
Output
The modified attached list is : [['1', 'geeks'], ['4', 'geeks'], ['5', 'geeks'], ['8', 'geeks'], ['3', 'geeks'], ['10', 'geeks']]
Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(n), where n is the length of the input list, because we are creating a new numpy array and a new list.
Associating a single value with all list items Using dictionary comprehension
Python3
test_list = [ 1 , 4 , 5 , 8 , 3 , 10 ]
val = 'geeks'
print ( "The original list is : " + str (test_list))
print ( "The value to be attached to each value : " + str (val))
res = {i: val for i in test_list}
res = list (res.items())
print ( "The modified attached list is : " + str (res))
|
Output
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')]
Time complexity: O(n) (where n is the length of the input list)
Auxiliary space: O(n) (where n is the length of the input list)
Share your thoughts in the comments
Please Login to comment...