
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Associating a single value with all list items in Python
We may have a need to associate a given value with each and every element of a list. For example − there are the name of the days and we want to attach the word day as a suffix in them. Such scenarios can be handled in the following ways.
With itertools.repeat
We can use the repeat method from itertools module so that the same value is used again and again when paired with the values from the given list using the zip function.
Example
from itertools import repeat listA = ['Sun','Mon','Tues'] val = 'day' print ("The Given list : ",listA) print ("Value to be attached : ",val) # With zip() and itertools.repeat() res = list(zip(listA, repeat(val))) print ("List with associated vlaues:\n" ,res)
Output
Running the above code gives us the following result −
The Given list : ['Sun', 'Mon', 'Tues'] Value to be attached : day List with associated vlaues: [('Sun', 'day'), ('Mon', 'day'), ('Tues', 'day')]
With lambda and map
The lambda method makes and iteration over the list elements and starts pairing them. The map function ensures all elements form the list are covered in pairing the list elements with the given value.
Example
listA = ['Sun','Mon','Tues'] val = 'day' print ("The Given list : ",listA) print ("Value to be attached : ",val) # With map and lambda res = list(map(lambda i: (i, val), listA)) print ("List with associated vlaues:\n" ,res)
Output
Running the above code gives us the following result −
The Given list : ['Sun', 'Mon', 'Tues'] Value to be attached : day List with associated vlaues: [('Sun', 'day'), ('Mon', 'day'), ('Tues', 'day')]
- Related Articles
- Set all list items on a single line with Bootstrap
- MySQL query to return all items in a single row
- Count number of items in a dictionary value that is a list in Python
- Insert the string at the beginning of all items in a list in Python
- Specify all the list properties into a single expression with CSS
- Program to count average of all special values for all permutations of a list of items in Python
- Python – Rows with all List elements
- MySQL query to increase item value price for multiple items in a single query?
- Find all triplets in a list with given sum in Python
- MySQL query to list all the items in a group in one record?
- Python – Strings with all given List characters
- How to randomize the items of a list in Python?
- Python Program to Multiply All the Items in a Dictionary
- Set all the columns of a MySQL table to a particular value with a single query
- How to preselect a value in a dropdown list of items in HTML forms?

Advertisements