EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials Python 3 Tutorial Python 3 Dictionary
Secondary Sidebar
Python 3 Tutorial
  • Python 3 Tutorial
    • Python 3 Commands
    • Python 3 Array
    • Python 3 Operators
    • Python 3 NumPy
    • Python 3 Webserver
    • Python 3 yield
    • Python 3 Pip
    • Python 3 Install
    • Python 3 raw_input
    • Python 3 HTTP Server
    • Python 3 Threading
    • Python 3 Requests
    • Python 3 Module Index
    • Python 3 String Format
    • Python 3 Unicode
    • Python 3 GUI
    • Python 3 xrange
    • Python 3 Tuple
    • Python 3 input
    • Python 3 JSON
    • Python 3 string
    • Python 3 try-except
    • Python 3 RegEx
    • Python 3 Object-Oriented Programming
    • Python 3 zip
    • Python 3 Exception
    • Python 3 write to file
    • Python 3 Functions
    • Python 3 List Function
    • Python 3 While Loop
    • Python 3 Global Variable
    • Python 3 String Methods
    • Python 3 interpreter
    • Python 3 REPL
    • Python 3 else if
    • Python 3 basics
    • Python 3 cheat sheet
    • Python 3 Print
    • Python 3 For Loop
    • Python 3 range
    • Python 3 Dictionary
    • Python 3 has_key
    • Python 3 URLlib

Python 3 Dictionary

Python 3 Dictionary

Definition of Python 3 Dictionary

Python 3 dictionary values data type replicated, however, keys cannot be copied and must be immutable. Keys of the dictionary is case-sensitive. With simply two curly braces, an empty dictionary with no objects can be written Values may or may not be unique within a dictionary. The keys of a dictionary must be immutable data types like texts, numbers, or tuples, whereas the contents can be of any kind.

Creating Python 3 Dictionary

Unlike other Data Types, which can only carry elements of a single value, Dictionary can hold key-value pairs. This means that even if the name is the same, different cases of Key will be interpreted differently.

The below example shows creating python 3 dictionaries are as follows.

1) In the below example we have created python 3 dictionaries with integer and mixed keys.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

All in One Software Development Bundle(600+ Courses, 50+ projects)
Python TutorialC SharpJavaJavaScript
C Plus PlusSoftware TestingSQLKali Linux
Price
View Courses
600+ Online Courses | 50+ projects | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (86,328 ratings)

Code:

# Creating dictionary by using integer Keys
Dic = {11: 'Python', 2: 'Dic', 3: 'Dictionary'}
print("\nCreating dictionary by using integer Keys: ")
print(Dic)
# Creating dictionary by using mixed keys
Dic = {'Name': 'Python', 1: [1, 2, 3, 4]}
print("\nCreating dictionary by using mixed keys: ")
print(Dic)

Output:

Creating Python 3 Dictionary 1

2) In the below example we are creating an empty dictionary and also creating a dictionary by using dict() method.

Code:

# Creating empty Dictionary by using dict method.
Dict = {}
print ("Creating Empty Dictionary: ")
print (Dict)
# Creating dictionary by using dict method.
Dict = dict ({1: 'Python', 2: 'dict', 3:'dictionary'})
print (Dict)

Output:

Creating Python 3 Dictionary 2

In the above example we have created an empty dictionary without passing any values of keys and pairs.

3) In the below example we are creating a nested dictionary are as follows.

Code:

# Example to create a Nested Dictionary by using python 3.
Dict = {1: 'Python', 2: 'Dictionary',
3:{'A' : 'Welcome', 'B' : 'To', 'C' : 'Python'}}
print (Dict)

Output:

Creating Python 3 Dictionary 3

Python 3 dictionary access

To get to the things in a dictionary, look up the key name. Inside square brackets, the key can be utilized.

1) In the below example we are accessing elements by using keys are as follows.

Code:

# Create dictionary by using python 3
Dict = {1: 'Python', 'name': 'dictionary', 3: 'dict'}
# accessing an element from dictionary by using key
print ("Access an element by using key:")
print (Dict ['name'])
print (Dict [1])

Output:

Creating Python 3 Dictionary 6

2) In the below example we are accessing elements from the python 3 dictionaries by using the get method are as follows.

Code:

# Create dictionary by using python 3
Dict = {1: 'Python', 'name': 'dictionary', 3: 'dict'}
# accessing an element from dictionary by using get method
print ("Access an element by using get method:")
print (Dict.get (3))
print (Dict.get (1))

Output:

Creating Python 3 Dictionary 7

3) In the below example we are accessing elements from python 3 by using a nested dictionary are as follows.

Code:

# Create dictionary by using python 3
Dic = {'Dic1': {1: 'Python'},
'Dic2': {'Name': 'Dictionary'}}
# accessing an element from dictionary by using key
print (Dic ['Dic1'])
print (Dic ['Dic1'][1])
print (Dic ['Dic2']['Name'])

Output:

Creating Python 3 Dictionary 9

Python 3 dictionary Elements

  • The addition of elements in Python Dictionary can be accomplished in a variety of ways. By defining value together with the key, such as Dict [Key] = ‘Value,’ we can also add a single value to the python 3 dictionaries.
  • The built-in update method can be used to replace an existing value in a Dictionary. An existing Dictionary can also have nested key values added.
  • If the key-value pair exists, the given value is updated, otherwise, a new Key is created with the value.
  • The elements of a dictionary are organized, changeable, and duplicates are not allowed. The key name can be used to refer to dictionary components that are given in key-value pairs.
  • When we state that our dictionary is in order, we’re referring to the fact that the entries are in a specific sequence that will not alter. Unordered objects don’t have a set order, thus we can’t use an index to find them.

Python 3 Dictionary Functions

Below are the functions available are as follows.

1. Len

This function gives us the total length of the dictionary. The length is equal to the number of elements from the dictionary.

The below example shows len functions in python are as follows.

Code:

Dic = {'Python': 18,'dict':12,'dictionary':22,'python':25}
print ("Length : %d" % len (Dic))

Output:

Len Functions

2. Str

This function produces the printable string are as follows.

Code:

Dic = {'Python': 18,'dict':12}
print (str (Dic))

Output:

Python 3 Dictionary Functions 2

3. Type

This function returns the type of a variable is as follows.

Code:

Dic = {'Python': 18,'dict':12}
print (type (Dic))

Output:

Python 3 Dictionary Functions 3

Python 3 Dictionary Method

Below is the method available as follows.

1. Copy

This method returns a shallow copy of the dictionary.

Code:

Dic = {1: 'Python', 'name': 'dictionary'}
Dic.copy()
print("copying Dictionary: ")
print(Dic)

Output:

Copy Method

2. Clear

This method will delete all the items from the dictionary.

Code:

Dic = {1: 'Python', 'name': 'dictionary'}
Dic.clear()
print("\nDeleting Dictionary: ")
print(Dic)

Output:

Clear Method

3. Pop

This method removes and returns element from the dictionary.

Code:

Dic = {1: 'Python', 'name': 'dictionary'}
pop_ele = Dic.pop(1)
print('\nDic after deletion: ' + str(Dic))
print('poped key is: ' + str(pop_ele))

Output:

Python 3 Dictionary pop

4. Popitem

This method will remove and returns the arbitrary element.

Code:

Dic = {1: 'Python', 'name': 'dictionary'}
pop_ele = Dic.popitem()
print('\nDic after deletion: ' + str(Dic))
print('poped key is: ' + str(pop_ele))

Output:

Python 3 Dictionary method Popitem

5. Get

This is conventional method used to access value from the key.

Code:

Dic = {1: 'Python', 'name': 'dictionary'}
print (Dic.get (1))

Output:

get

Conclusion

The dictionary includes key-value pairs. It can be formed in Python by enclosing a succession of entries in curly braces and separating them with a comma. The dictionary stores the pair Key and the other is the Key value pair element.

Recommended Articles

This is a guide to Python 3 Dictionary. Here we discuss the definition, Creating Python 3 Dictionary, Python 3 dictionary access, Examples with code implementation. You may also have a look at the following articles to learn more –

  1. JavaScript Dictionary
  2. PowerShell Dictionary
  3. TypeScript Dictionary
  4. Python Add List
Popular Course in this category
Python Certifications Training Program (40 Courses, 13+ Projects)
  40 Online Courses |  13 Hands-on Projects |  215+ Hours |  Verifiable Certificate of Completion
4.8
Price

View Course
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

C# Programming, Conditional Constructs, Loops, Arrays, OOPS Concept

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more