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
# 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 = 'Lazyroar' # 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 : Lazyroar The modified attached list is : [(1, 'Lazyroar'), (4, 'Lazyroar'), (5, 'Lazyroar'), (8, 'Lazyroar'), (3, 'Lazyroar'), (10, 'Lazyroar')]
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
# 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 = 'Lazyroar' # 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 : Lazyroar The modified attached list is : [(1, 'Lazyroar'), (4, 'Lazyroar'), (5, 'Lazyroar'), (8, 'Lazyroar'), (3, 'Lazyroar'), (10, 'Lazyroar')]
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
# Python3 code to demonstrate # associate value in list # initializing list test_list = [ 1 , 4 , 5 , 8 , 3 , 10 ] # initializing value to associate val = 'Lazyroar' # 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)) res = [] for i in test_list: x = [i, "Lazyroar" ] res.append( tuple (x)) # 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 : Lazyroar The modified attached list is : [(1, 'Lazyroar'), (4, 'Lazyroar'), (5, 'Lazyroar'), (8, 'Lazyroar'), (3, 'Lazyroar'), (10, 'Lazyroar')]
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 = 'Lazyroar' # Use zip() and a generator expression to create a list of tuples with the elements # of test_list and a sequence of val values. res = list ( zip (test_list, (val for _ in test_list))) # Print the original list and value print ( "The original list is :" , test_list) print ( "The value to be attached to each value :" , val) # Print the modified attached list print ( "The modified attached list is :" , res) #This code is contributed by Edula Vinay Kumar Reddy |
The original list is : [1, 4, 5, 8, 3, 10] The value to be attached to each value : Lazyroar The modified attached list is : [(1, 'Lazyroar'), (4, 'Lazyroar'), (5, 'Lazyroar'), (8, 'Lazyroar'), (3, 'Lazyroar'), (10, 'Lazyroar')]
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 = 'Lazyroar' res = [(i, val) for i in test_list] print ( "The modified attached list is : " + str (res)) |
The modified attached list is : [(1, 'Lazyroar'), (4, 'Lazyroar'), (5, 'Lazyroar'), (8, 'Lazyroar'), (3, 'Lazyroar'), (10, 'Lazyroar')]
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 = 'Lazyroar' # convert list to numpy array arr = np.array(test_list) # create numpy array with same length as input list, and fill with given string value str_arr = np.full(( len (test_list),), val) # horizontally stack both arrays res_arr = np.column_stack((arr, str_arr)) # convert resulting numpy array to list res_list = res_arr.tolist() # print the resulting list print ( "The modified attached list is : " + str (res_list)) |
Output
The modified attached list is : [['1', 'Lazyroar'], ['4', 'Lazyroar'], ['5', 'Lazyroar'], ['8', 'Lazyroar'], ['3', 'Lazyroar'], ['10', 'Lazyroar']]
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
# initializing list test_list = [ 1 , 4 , 5 , 8 , 3 , 10 ] # initializing value to associate val = 'Lazyroar' # 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 dictionary comprehension # associate value in list res = {i: val for i in test_list} # converting dictionary to list of tuples res = list (res.items()) # 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 : Lazyroar The modified attached list is : [(1, 'Lazyroar'), (4, 'Lazyroar'), (5, 'Lazyroar'), (8, 'Lazyroar'), (3, 'Lazyroar'), (10, 'Lazyroar')]
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)