
- 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
How do I assign a dictionary value to a variable in Python?
You can assign a dictionary value to a variable in Python using the access operator [].
example
my_dict = { 'foo': 42, 'bar': 12.5 } new_var = my_dict['foo'] print(new_var)
Output
This will give the output −
42
example
This syntax can also be used to reassign the value associated with this key.
my_dict = { 'foo': 42, 'bar': 12.5 } my_dict['foo'] = "Hello" print(my_dict['foo'])
Output
This will give the output −
Hello
- Related Articles
- How do we assign a value to several variables simultaneously in Python?
- How to assign multiple values to a same variable in Python?
- How do I serialize a Python dictionary into a string, and then back to a dictionary?
- Can we assign a reference to a variable in Python?
- How to assign int value to char variable in Java
- How do I declare a global variable in Python class?
- How can we assign a bit value as a number to a user variable?
- How do I format a string using a dictionary in Python 3?
- How do I check if a Python variable exists?
- How to assign a reference to a variable in C#
- How to assign a PHP variable to JavaScript?
- How do I call a Variable from Another Function in Python?
- Assign other value to a variable from two possible values in C++
- How can we assign a function to a variable in JavaScript?
- How do I pass a variable to a MySQL script?

Advertisements