TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to PYTHON

Python dict Keyword (Copy Dictionary)

Use the dict keyword to create dictionaries with other dictionaries and lists. Create an empty dictionary.
Dict. In Python a dictionary can be created with an expression in curly brackets. But the dict built-in method can also be used. This provides more initialization options.Built-ins
With dict, we create an empty dictionary. We create a dictionary from another dictionary (copying it). We create one from a list.
Empty dictionary example. Here we create an empty dictionary with dict(). No keys or values are inside the dictionary. Its len is 0.Len

Then: We add a key "cat" to the dictionary with a value of 1. Then we use get() to print this value.

Python program that uses dict # Use dict to create empty dictionary. values = dict() # Add value and get value. values["cat"] = 1 print(values.get("cat")) Output 1
Copy dictionary. Here we copy a dictionary with dict. We create a dictionary called values and then copy it into a dictionary called "copy."

Tip: When we modify the copied dictionary, the original dictionary is left unchanged. The two are separate in memory.

Python program that copies with dict built-in values = {"cat": 1, "bird": 2} # Copy the dictionary with the dict copy constructor. copy = dict(values) # Change copy. copy["cat"] = 400 # The two dictionaries are separate. print(values) print(copy) Output {'bird': 2, 'cat': 1} {'bird': 2, 'cat': 400}
List, dict. A list of tuples (each a key-value pair) can be used to construct a dictionary. Dict turns these pairs into keys and values. This is a way to transform a list into a dictionary.ListDictionaryConvert
Python program that creates list with dict # Create a dictionary with dict based on a list of pairs. # ... List contains tuples with keys and values. values = [("cat", 1), ("bird", 200)] lookup = dict(values) print(values) print(lookup.get("cat")) print(lookup.get("bird")) Output [('cat', 1), ('bird', 200)] 1 200
Named arguments. Python supports named arguments. In this syntax, arguments are passed with an equals sign statement. A dictionary can be constructed with this syntax.Def
Python program that uses named arguments # Create a dictionary with named arguments. animals = dict(bird=1, dog=2, fish=9) print(animals.get("bird")) print(animals.get("dog")) print(animals.get("fish")) Output 1 2 9
Dict TypeError. The dict keyword must not be incorrectly used. If we provide incorrect arguments, a TypeError will interrupt our program's execution.
Python program that causes error # The dict built-in must be called with a valid argument. result = dict(1) Output Traceback (most recent call last): File "C:\programs\file.py", line 5, in <module> result = dict(1) TypeError: 'int' object is not iterable
With this function, we create a powerful dictionary collection from varied arguments. Often with dictionaries we do not need a dict() call. But when we do, dict simplifies our code.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf