my_keys=('a','b','c') # using a tuple
my_values=('ONe','Two','Three')
my_dict=dict.fromkeys(my_keys,my_values)
print(my_dict)
Output is here
{'a': ('ONe', 'Two', 'Three'), 'b': ('ONe', 'Two', 'Three'),
'c': ('ONe', 'Two', 'Three')}
my_keys=('a','b','c') # using a tuple
my_dict=dict.fromkeys(my_keys)
print(my_dict)
Output
{'a': None, 'b': None, 'c': None}
my_keys=('a','b','c') # using a tuple
my_values=('One')
my_dict=dict.fromkeys(my_keys,my_values)
print(my_dict)
Output
{'a': 'One', 'b': 'One', 'c': 'One'}
my_keys=range(1,6) # using a range to get sequence of keys
my_dict=dict.fromkeys(my_keys)
print(my_dict)
Output
{1: None, 2: None, 3: None, 4: None, 5: None}
We can assign value to any key
my_dict[2]='Two'
print(my_dict)
Output
{1: None, 2: 'Two', 3: None, 4: None, 5: None}
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.