- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 10784 Articles for Python
![Arjun Thakur](https://www.tutorialspoint.com/assets/profiles/13574/profile/60_74441-1560315595.jpg)
432 Views
The JSON module is a very reliable library to serialize a Python dictionary into a string, and then back to a dictionary. The dumps function converts the dict to a string. exampleimport json my_dict = { 'foo': 42, 'bar': { 'baz': "Hello", 'poo': 124.2 } } my_json = json.dumps(my_dict) print(my_json)OutputThis will give the output −'{"foo": 42, "bar": {"baz": "Hello", "poo": 124.2}}'The loads function converts the string back to a dict. exampleimport json my_str = '{"foo": 42, "bar": {"baz": "Hello", "poo": 124.2}}' my_dict = json.loads(my_str) print(my_dict['bar']['baz'])OutputThis will give the output −Hello
![Lakshmi Srinivas](https://www.tutorialspoint.com/assets/profiles/13528/profile/60_91717-1512651120.jpg)
27K+ Views
It is pretty easy to get the sum of values of a Python dictionary. You can first get the values in a list using the dict.values(). Then you can call the sum method to get the sum of these values. exampled = { 'foo': 10, 'bar': 20, 'baz': 30 } print(sum(d.values()))OutputThis will give the output −60
![Arjun Thakur](https://www.tutorialspoint.com/assets/profiles/13574/profile/60_74441-1560315595.jpg)
4K+ Views
You can convert Python dictionary keys/values to lowercase by simply iterating over them and creating a new dict from the keys and values. For example, def lower_dict(d): new_dict = dict((k.lower(), v.lower()) for k, v in d.items()) return new_dict a = {'Foo': "Hello", 'Bar': "World"} print(lower_dict(a))This will give the output{'foo': 'hello', 'bar': 'world'}If you want just the keys to be lower cased, you can call lower on just that. For example, def lower_dict(d): new_dict = dict((k.lower(), v) for k, v in d.items()) return new_dict a = {'Foo': "Hello", 'Bar': "World"} print(lower_dict(a))This will give the output{'foo': 'Hello', 'bar': ... Read More
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
1K+ Views
Python and javascript both have different representations for a dictionary. So you need an intermediate representation in order to pass data between them. The most commonly used intermediate representation is JSON, which is a simple lightweight data-interchange format.ExampleThe dumps function converts the dict to a string. For example, import json my_dict = { 'foo': 42, 'bar': { 'baz': "Hello", 'poo': 124.2 } } my_json = json.dumps(my_dict) print(my_json)OutputThis will give the output:'{"foo": 42, "bar": {"baz": "Hello", "poo": 124.2}}'ExampleThe load's function converts the string back to a dict. For example, import json my_str ... Read More
![Ankith Reddy](https://www.tutorialspoint.com/assets/profiles/22328/profile/60_142162-1519038074.jpg)
1K+ Views
A python dictionary is a Hashmap. You can use the map data structure in C++ to mimic the behavior of a python dict. You can use map in C++ as follows:#include #include using namespace std; int main(void) { /* Initializer_list constructor */ map m1 = { {'a', 1}, {'b', 2}, {'c', 3}, {'d', 4}, {'e', 5} }; cout
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
349 Views
There are many C++/Python bindings. It boils down to what you use to communicate between C++ and python to read python dictionaries in c++. Most of these libraries(like Boost) handle the parsing themselves. You could use an intermediate data transfer format like JSON or XML to pass data between the 2 languages and then serialize and deserialize data using the respective libraries in these languages for these formats.
![Lakshmi Srinivas](https://www.tutorialspoint.com/assets/profiles/13528/profile/60_91717-1512651120.jpg)
129 Views
You can sort a list of dictionaries by values of the dictionary using the sorted function and passing it a lambda that tells which key to use for sorting. For example, A = [{'name':'john', 'age':45}, {'name':'andi', 'age':23}, {'name':'john', 'age':22}, {'name':'paul', 'age':35}, {'name':'john', 'age':21}] new_A = sorted(A, key=lambda x: x['age']) print(new_A)This will give the output:[{'name': 'john', 'age': 21}, {'name': 'john', 'age': 22}, {'name': 'andi', 'age': 23}, {'name': 'paul', 'age': 35}, {'name': 'john', 'age': 45}]You can also sort it in place using the sort function instead of the sorted function. For example, A ... Read More
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
2K+ Views
If you have a dictionary of the following format:{ 'KEY1':{'name':'foo', 'data':1351, 'completed':100}, 'KEY2':{'name':'bar', 'data':1541, 'completed':12}, 'KEY3':{'name':'baz', 'data':58413, 'completed':18} }And you want to sort by the key, completed within each entry, in a ascending order, you can use the sorted function with a lambda that specifies which key to use to sort the data. For example, my_collection = { 'KEY1':{'name':'foo', 'data':1351, 'completed':100}, 'KEY2':{'name':'bar', 'data':1541, 'completed':12}, 'KEY3':{'name':'baz', 'data':58413, 'completed':18} } sorted_keys = sorted(my_collection, key=lambda x: (my_collection[x]['completed'])) print(sorted_keys)This will give the output:['KEY2', 'KEY3', 'KEY1']Read More