Open In App

Associating a Single Value with All List Items - Python

Last Updated : 11 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

We are given a list and we need to attach the same value to each element in a list creating a new list with tuples of the original element and the associated value.

For example: Given the list [1, 2, 3] and the value 'x', the result would be [(1, 'x'), (2, 'x'), (3, 'x')].

Using map()

This method uses the map() function to apply a lambda function that pairs each element with the given value.

Python
li = [1, 4, 5, 8, 3, 10]
val = 'geeks'

# Using map() + lambda to associate value with list elements
res = list(map(lambda i: (i, val), li))

print(res)

Output
[(1, 'geeks'), (4, 'geeks'), (5, 'geeks'), (8, 'geeks'), (3, 'geeks'), (10, 'geeks')]

Let's explore other methods to achieve the same:

Using zip() and itertools.repeat()

zip() function pairs elements from the list with repeated values using itertools.repeat() to create a tuple for each list item.

Python
from itertools import repeat

li = [1, 4, 5, 8, 3, 10]
val = 'geeks'

# Using zip() + itertools.repeat to associate value with list elements
res = list(zip(li, repeat(val)))

print(res)

Output
[(1, 'geeks'), (4, 'geeks'), (5, 'geeks'), (8, 'geeks'), (3, 'geeks'), (10, 'geeks')]

Explanation: repeat() creates an infinite sequence of the value val, which is then paired with each element from the list li using zip().

Using a for loop and tuple()

For loop is used to create tuples of each element and the specified value.

Python
li = [1, 4, 5, 8, 3, 10]
val = 'geeks'

# Using for loop and tuple() to associate value with list elements
res = []
for s in li:
    res.append((s, val))
    
print(res)

Output
[(1, 'geeks'), (4, 'geeks'), (5, 'geeks'), (8, 'geeks'), (3, 'geeks'), (10, 'geeks')]

Using a list comprehension

List comprehensions in Python provide a concise way to create lists by applying an expression to each item in an existing iterable. If we want to associate a single value with all items in a list, we can achieve this in several ways depending on how we want to structure the association.

Python
li = [1, 4, 5, 8, 3, 10]
val = 'geeks'

# Using list comprehension to associate value with list elements
res = [(i, val) for i in li]

print(res)

Output
[(1, 'geeks'), (4, 'geeks'), (5, 'geeks'), (8, 'geeks'), (3, 'geeks'), (10, 'geeks')]

Explanation: Each element i is iterated over in li and paired with the value val to create a list of tuples

Using numpy library

numpy library is used to create a numpy array which is then combined with a repeated array of the associated value.

Python
import numpy as np

li = [1, 4, 5, 8, 3, 10]
v = 'geeks'

arr = np.array(li)
str_arr = np.full((len(li),), v)
res_arr = np.column_stack((arr, str_arr))

# Convert the numpy array to a list
res = res_arr.tolist()

print("The modified attached list is:", res)

Output
The modified attached list is: [['1', 'geeks'], ['4', 'geeks'], ['5', 'geeks'], ['8', 'geeks'], ['3', 'geeks'], ['10', 'geeks']]

Explanation:

  • np.full() creates an array of repeated values.
  • np.column_stack() horizontally stacks the original list with the repeated values creating a structured array

Using Dictionary Comprehension

With dictionary comprehension, we can create a dictionary where each key-value pair consists of an item from the list and the associated value

Python
li = [1, 4, 5, 8, 3, 10]
v = 'geeks'

res = {i: v for i in li}

# Convert dictionary to list of tuples
res = list(res.items())

print("The modified attached list is:", res)

Output
The modified attached list is: [(1, 'geeks'), (4, 'geeks'), (5, 'geeks'), (8, 'geeks'), (3, 'geeks'), (10, 'geeks')]

Similar Reads