Sometimes, while working with dictionaries, we might have an utility in which we need to initialize a dictionary with records values, so that they can be altered later. This kind of application can occur in cases of memoizations in general or competitive programming. Letâs discuss certain way in which this task can be performed.
Method : Using zip() + repeat()
The combination of these functions can be used to perform this particular task. In this, the Dictionary value is attached to the keys repeated using the repeat() by help of zip()
# Python3 code to demonstrate working of # Dictionary initialization with common dictionary # Using zip() + repeat() from itertools import repeat # initialization Dictionary test_dict = {'gfg' : 1, 'best' : 3} # Using zip() + repeat() # Dictionary initialization with common dictionary res = dict(zip(range(4), repeat(test_dict))) # printing result print("The dictionary with record values : " + str(res)) |
The dictionary with record values : {0: {'gfg': 1, 'best': 3}, 1: {'gfg': 1, 'best': 3}, 2: {'gfg': 1, 'best': 3}, 3: {'gfg': 1, 'best': 3}}
Recommended Posts:
- Python - Custom dictionary initialization in list
- Python - Incremental value initialization in Dictionary
- Counters in Python | Set 1 (Initialization and Updation)
- Python - Incremental K sized Row Matrix Initialization
- Python | Boolean list initialization
- Python | List Initialization with alternate 0s and 1s
- Python | Interval Initialization in list
- Python - K Matrix Initialization
- Python - Incremental Range Initialization in Matrix
- Python | Find common elements in three sorted arrays by dictionary intersection
- Python | Combine two dictionary adding values for common keys
- Python | Initialize dictionary with common value
- Python - Common items Dictionary Value List
- Python - Common list elements and dictionary values
- Python - Common keys in list and dictionary
- Python | Convert flattened dictionary into nested dictionary
- Python | Convert nested dictionary into flattened dictionary
- Python | Convert string dictionary to dictionary
- Python | Pretty Print a dictionary with dictionary value
- Regular Dictionary vs Ordered Dictionary in Python
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.

