TheDeveloperBlog.com

Home | Contact Us

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

Python dict() function with Examples

Python dict() function with Examples on append(), clear(), extend(), insert(), pop(), remove(), index(), count(), pop(), reverse(), sort(), copy(), all(), bool(), enumerate(), iter(), map(), min(), max(), sum() etc.

<< Back to PYTHON

Python dict() Function

Python dict() function is a constructor which creates a dictionary. Python dictionary provides three different constructors to a create dictionary.

  • If no argument is passed, it creates an empty dictionary.
  • If a positional argument is given, a dictionary is created with the same key-value pairs. Otherwise, pass an iterable object.
  • If keyword arguments are given, the keyword arguments and their values are added to the dictionary created from the positional argument.

Signature

dict ([**kwargs])
dict ([mapping, **kwargs])
dict ([iterable, **kwargs])

Parameters

kwargs: It is a keyword argument.

mapping: It is another dictionary.

iterable: It is an iterable object in the form of a key-value pair(s).

Return

It returns a dictionary.

Let's see some examples of dict() function to understand it's functionality.

Python dict() Function Example 1

A simple example to create an empty or non-empty dictionary. Arguments of the dictionary are optional.

# Python dict() function example
# Calling function
result = dict() # returns an empty dictionary
result2 = dict(a=1,b=2)
# Displaying result
print(result)
print(result2)

Output:

{}
{'a': 1, 'b': 2}

Python dict() Function Example 2

# Python dict() function example
# Calling function
result = dict({'x': 5, 'y': 10}, z=20) # Creating dictionary using mapping
result2 = dict({'x': 5, 'y': 10, 'z':20})
# Displaying result
print(result)
print(result2)

Output:

{'x': 5, 'z': 20, 'y': 10}
{'x': 5, 'z': 20, 'y': 10}

Python dict() Function Example 3

# Python dict() function example
# Calling function
result = dict([(1, 'One'), [2, 'Two'], [3,'Three']]) # Creating using iterable
result2 = dict([['x','X'],('y','Y')])
# Displaying result
print(result)
print(result2)

Output:

{1: 'One', 2: 'Two', 3: 'Three'}
{'y': 'Y', 'x': 'X'}

Next TopicPython Functions




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