Change language

Python Dictionary | The fromkeys () method

| |

Sometimes it becomes necessary to form a dictionary from the given keys. Roughly doing this would take time and be more tedious work. Hence, fromkeys () helps us to easily solve this problem using just one method. This article explains the operational and other aspects associated with this feature.

Syntax: fromkeys (seq, val)

Parameters:
seq: The sequence to be transformed into a dictionary.
val: Initial values ​​that need to be assigned to the generated keys ... Defaults to None.

Returns: A dictionary with keys mapped to None if no value is provided, else to the value provided in the field.

Code # 1: Demonstration of fromkeys ()

# Python 3 demo code
# working with keys ()

 
# initialization sequence

seq = { ’a’ , ’ b’ , ’c’ , ’ d’ , ’e’ }

  
# using fromkeys () to convert sequence to dict
# initialize with using None

res_dict = dict . fromkeys (seq)

 
# Print dict created

print ( "The newly created dict with None values:" + str (res_dict ))

 

 
# using fromkeys () to convert sequence to dict
# initialize with 1

res_dict 2 = dict . Fromkeys (seq, 1 )

 
# Print dict created

print ( "The newly created dict with 1 as value:" + str (res_dict2))

Exit :

The newly created dict with None values: {’d’: None, ’a’: None, ’b’: None, ’c’: None, ’e ’: None}
The newly created dict with 1 as value: {’ d ’: 1,’ a ’: 1,’ b ’: 1,’ c ’: 1,’ e ’: 1}

The fromdict () behavior with mutable objects as values:

The fromdict () function can also be supplied with a multilatable object as a s default values. But in this case, a deep copy is created from the dictionary, that is, if we add a value to the original list, the addition occurs in all key values.

Prevention: Certain dictionary comprehension techniques can be used to creating a new list as key values ​​that does not point to the original list as key values.

Code # 2: Demonstrate behavior with mutable objects.

# Python 3 demo code
# behavior with mutable objects

 
# sequence and list initialization

seq = { ’a’ , ’ b’ , ’c’ , ’d’ , ’ e’ }

lis1 = [ 2 , 3 ]

 
# using fromkeys () to convert sequence to dict
# using normal method

res_dict = dict . fromkeys (seq, lis1)

 
# Print created dict

print ( "The newly created dict with list values:"

+ str (res_dict))

 
# add to lis1

lis1.append ( 4 )

  
# Print diktat after adding
# Note that adding occurs in all values ​​

print ( " The dict with list values ​​after appending: "

+ str (res_dict) )

 

lis1 = [ 2 , 3 ]

print ( ’’ )

 
# using fromkeys () to convert sequence to dict
# using dict. understanding

res_dict2 = {key: list (lis1) for key in seq}

  
# Print created dict

print ( " The newly created dict with list values: " 

  + str (res_dict2))

 
# add to lis1

lis1.append ( 4 )

 
# Print diktat after adding
# Note that adding is not happening now.

print ( " The dict with list values ​​after appending (no change): "

+ str (res_dict2))

Output:

The newly created dict with list values: {’d’: [2, 3], ’e’: [2, 3], ’c’: [2, 3] , ’a’: [2, 3], ’b’: [2, 3]}
The dict with list values ​​after appending: {’d’: [2, 3, 4], ’e’: [2, 3, 4], ’c’: [2, 3, 4], ’a’: [2, 3, 4], ’b’: [2, 3, 4]}

The newly created dict with list values: {’d’: [2, 3], ’e’: [2, 3], ’c’: [2, 3], ’a’: [2, 3], ’b’: [2, 3]}
The dict with list values ​​after appending (no change): {’d’: [2, 3], ’e’: [2, 3], ’c’ : [2, 3], ’a’: [2, 3], ’b’: [2, 3]}

Python Dictionary | The fromkeys () method __dict__: Questions

Python Dictionary | The fromkeys () method fromkeys: Questions

Shop

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method