×

Python Dictionary setdefault() method

Python Dictionary setdefault() method

The dictionary.setdefault () method in Python returns the value of the item with the specified key.

Syntax

dictionary.setdefault(keyname, value)

Parameter

keyname- This parameter represents the keyname of the item you want to return the value.

value(optional)- This parameter represents a Key with a value default_value is inserted to the dictionary. If no value is provided, the default_value will be none. 

Return

This method returns an arbitrary element (key, value) pair from the given dictionary.

Example 1

 # Python program explaining
 # the dictionary.setdefault() method
 # initialising the dictionary
 fruits = {
  "banana" : "vitamins",
   "orange": "citric acid",
   "grapes": 5
 }
 # printing the Dictionary
 print("Dictionary:",fruits)
 # returning the value of the key.
 defaultValue=fruits.setdefault("grapes","vitamin c")
 print("Value of the key: \n", defaultValue) 

Output

Dictionary: {'grapes': 5, 'orange': 'citric acid', 'banana': 'vitamins'}
Value of the key: 5

Example 2

 # Python program explaining
 # the dictionary.setdefault() method
 # initializing the dictionary
 student = {'studentName': 'Reema','Roll No':'15cs1029'}
 # The given key is not in the dictionary
 branch = student.setdefault('branch')
 print('Student Details = ',student)
 print('Branch = ',branch)
 # The specified key is not in the dictionary
 # default_value is provided
 age = student.setdefault('age', 22)
 print('Student Details = ',student)
 print('age = ',age) 

Output

 Student Name =  {'Roll No': '15cs1029', 'branch': None, 'studentName': 'Reema'}
 Branch =  None
 Student Name =  {'Roll No': '15cs1029', 'age': 22, 'branch': None, 'studentName': 'Reema'}
 age =  22 

Related Topics

List Index out of Range Python for Loop

The List is generally used in Python to store multiple items or elements inside one single variable. The List is ordered in nature, the List can contain the elements inside...

3 minutes read.

Python Logging Maxbytes

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

6 minutes read.

Python comment symbol

Python programmers frequently use the comment system because, without it, things may quickly become very perplexing. The developers' helpful information is provided in the comments, which helps the reader understand...

3 minutes read.

Reverse a String in Python

Python is an object-oriented high-level programming language. Python has dynamic semantics and has high-level built-in data structures which support dynamic typing and dynamic binding. Python provides rapid development. It has...

4 minutes read.

Python Set symmetric_difference_update() method

Python Set symmetric_difference_update() method The set.symmetric_difference_update() method in Python updates the original set by removing items that are present in both sets and inserting the other items of the set. Syntax set.symmetric_difference_update(set1) Parameter set- This parameter represents the first...

2 minutes read.

How to Parse JSON in Python

JSON The JSON, the Java Script Object Notation, is an open standard file designed for data interchange; it is light-weighted text. The JSON uses human-readable text to store and transmit the...

4 minutes read.

BOTTLE Python Web Framework

The Bottle is a lightweight WSGI micro web framework for python. It acts like a thin wrapper around a web server where it is distributed as a single file module...

4 minutes read.

Python Combination

Python Combination Permutations and combinations are the important part of our mathematical tools. We use them often in our Python programs. In this tutorial, we will learn about combinations in Python...

6 minutes read.

Python data science course

What is meant by Data Science? When processing raw, structured, and unstructured data utilizing various technologies, algorithms, and the scientific method, data science is a detailed study of the enormous quantity...

4 minutes read.

Speech Recognition in Python

What is Speech Recognition? Speech Recognition is a term defined for automatic recognition of human speech. Speech recognition is the most significant activity in the domain of the interaction between the...

8 minutes read.

Python vs R

Python: - Python is a popular high-level, general-purpose programming language. Python (the most recent version is Python 3) is a programming language used in the software industry for website development, machine...

3 minutes read.

Python String Methods

Python String Methods Python has a set of built-in string methods. The Python string methods are as follows: capitalize() The string.capitalize() method returns a copy of the string by converting the...

5 minutes read.

Python Simple Interest

Python is an Object-Oriented high-level language. Python has an English-like syntax, which is very easy to read and write codes. Python is an interpreted language which means that it uses...

3 minutes read.

Python Linear regression

In this tutorial, we will understand the meaning, usage, and types of Linear Regression. Further, we will comprehend the terms cost function and Optimization. Linear Regression is the widely used Machine...

3 minutes read.

Creating Tables using Python MySQL

In this article, we are going to learn how to create tables in databases using Python MySQL. Introduction to Tables: Generally, databases are used in order to store the information in the...

9 minutes read.

How to check version of Python

How to check version of python The versions of Python come with different kinds of features and functionalities. It is not a herculean task to keep track of the updates these...

3 minutes read.

Python ord() Function

Python ord() Function The ord() function in Python returns the number representing the Unicode code of a specified character. Syntax ord(c) Parameter c: This parameter represents a string or any character. Return This function returns the number...

1 minute read.

How to Create an Array in Python?

How to Create an Array in Python In the previous article, we have discussed how to declare an array in Python. So, let's have a quick revision of that, and then we...

5 minutes read.

Introduction to Scratch programming

Scratch programming is generally designed for children who may create digital stories, games, and animations using the computer language Scratch, which has the largest kid-focused community in the world. Scratch...

3 minutes read.

Explain sklearn clustering in Python

Make a connection and patterns across datasets by using clustering, one of the unsupervised machine learning approaches. Grouping is crucial because it ensures unlabelled data's natural clustering. The sample from...

7 minutes read.