×

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

Python format() function

Python format() function The format() function in Python formats a specified value into the given format. A ‘TypeErrorexception’ is raised if the method search reaches the object and the format_spec is non-empty, or if either the format_spec or the...

1 minute read.

Python String join() method

Python String join() method The String.join() method in Python concatenates each element of an iterable (such as list, string and tuple) to the given string and returns the concatenated string. Syntax string.join(seq) Parameter Seq: This...

1 minute read.

Multiple Linear Regression using Python

Linear Regression: Linear regression is a method that models the relationship between a dependent variable and one or more independent variables; in other terms, that models the relationship between a target...

6 minutes read.

Kite Python

Kite in Python: The Kite is a package provided by the python programming language; it works with the help of artificial intelligence and helps us write code inside the visual studio....

3 minutes read.

Operator Module In Python

Introduction The operator module is used for performing operations using methods rather than utilizing operators in Python code. The operator module provides several methods for performing the operations.  The operator module contains...

7 minutes read.

How to create a file in Python?

How to create a file in python? Files provide us a convenient way of storing our information. Once we have our data, the next task is to store it so that...

3 minutes read.

Collections in python

In this tutorial, we will see what is collection in python.  Further, we will see in-depth the different kinds of collections in python. Collections Collections in python are the built-in module used...

9 minutes read.

Python coding platform

Python is a popular general-purpose programming language with many applications. High-level data structures, datatypes, dynamic binding, and many other features make it useful for both designing complex applications and "glue...

6 minutes read.

Python Hash Table

An Introduction to Hashing A technique which used to identify a particular object uniquely from a set of similar objects is known as Hashing. Some real-life examples of hashing implementations include: A...

9 minutes read.

Array to String in Python

What is an Array: The idea behind an array is to group several items of the same type, making it easier to locate each element by adding an extra offset to...

4 minutes read.

Python Array

Python Array In the programming language or computer science, an array is defined as the form of a data structure which consists of or store collection of various types of elements...

10 minutes read.

EDA in Python

The EDA is the exploratory data analysis; the data scientists mainly use the EDA to understand the main features of the data quickly, the variables in python and the relationship...

6 minutes read.

Installing Packages in Python

It's critical to understand that the term "package" here refers to a collection of software that must be installed (i.e. as a synonym for a distribution). It has nothing to...

6 minutes read.

How to Make an App with Python

In this age of mobiles, rapid mobile application development has got a lot of traction. Moreover, app developers are high in demand because of the increase in digitization. Generally, Python is...

4 minutes read.

Best books for NLP with Python

NLP NLP – Natural language processing Computers were created to make the life of mankind easier. But computers cannot understand human language. Instead, they interact with numbers to perform the tasks allotted...

12 minutes read.

Python Dictionary items() method

Python Dictionary items() method The dictionary.items() method in Python returns a view object that displays a list of dictionary's (key, value) tuple pairs. Syntax dictionary.items() Parameter NA Return This method returns a view object, displaying the list...

1 minute read.

Python enumerate() function

Python enumerate() function The enumerate() function in Python takes a collection (e.g. a tuple) and returns it as an enumerate object. Syntax: enumerate(iterable, start=0) Parameter Iterable:  This parameter represents an iterable object start:    This parameter represents a number defining...

1 minute read.

Find Median of List in Python

The median is an enlightening measurement that is utilized as a proportion of the focal inclination of a circulation. It is equivalent to the centre worth of the conveyance. There...

3 minutes read.

How to create a list in Python?

How to create a list in python Python is known for its versatility and its data structures help us to perform different kinds of operations on various datasets irrespective of their...

4 minutes read.

Python Program to Check Leap Year

Python Program to Check Leap Year Leap year: We all know that each year has 365 or 366 days. But whenever a year has 366 days, then the year is said...

2 minutes read.