×

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 Project Ideas for Beginners

Python project ideas for beginners Advanced Python is an amazing platform to grow and achieve success as a developer in the future. Python is a programming language that can be very...

7 minutes read.

Binary Search Visualization using Pygame in Python

A calculation like Binary Search is seen effectively by picturing. Here in this tutorial, a function that pictures the Binary Search Algorithm is carried out. The Graphical User’s Interface (GUI)...

15 minutes read.

Python SymPy

A Python package for symbolic mathematics is called SymPy. Its goal is to develop into a fully-fledged computer algebra system (CAS) while maintaining the code as straightforward as possible to...

3 minutes read.

How to Install Scikit-Learn

Sklearn or Scikit-learn is a python library used for machine learning. It contains many features like classification, regression, clustering, and Dimensionality reduction algorithms. Sklearn is used to build machine learning...

3 minutes read.

How to open a file in python

Opening a File in Python Python is a user-friendly programming language that makes almost every concept easy. It provides many inbuilt functions in its libraries to work with files. Using these...

6 minutes read.

Image to Text in python

Processing out information from an image is a very big task in all the fields of work such as in development and business sector. The process of converting an electronical...

3 minutes read.

Python String rindex() method

Python String rindex() method The string. rindex() method in Python returns the highest index of the substring inside the string (if found). If the substring is not found, it raises an...

2 minutes read.

Sort a dataframe based on a column in Python

Sorting the dataframe based on a column requires pandas which is An open-source library called Python Pandas is described as offering high-performance data processing in Python. For both professionals and...

4 minutes read.

__init__ in python

Every programmer will enclose to object-oriented programming (OOP) these days. As we know, that python is the latest and most modern programming language; python supports to implementation of object-oriented philosophy....

5 minutes read.

Python float()

Python float() class The float() class in Python returns a floating point number constructed from a number or string x. Syntax class float([x]) Parameter x: This parameter represents a number or a string that can be converted...

1 minute read.

Python String Negative Indexing

This page contains all the information how to use “Negative indexing“ in Python with the help of using slicing. What is an Index? An index is a numeric value, which is assigned...

3 minutes read.

Standard GUI Unit Converter using PyQt5 in Python

GUI: A graphical interface (GUI) is a user interface that lets users interact with electronic devices like computers and smartphones by using menus, icons, and other visual cues (graphics). In contrast...

6 minutes read.

Python List Methods

Python List Methods Python has a set of built-in methods that you can use on lists or arrays. Following are all of the methods of list objects: Methods Explanation append The list.append() method...

3 minutes read.

Python Program to find the length of a string

Python program to find the length of a string A string in Python is a set of Unicode characters. It can't be changed once it's been declared. The number of characters...

2 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 e-book free download

Python is a booming language these days. It has many applications for making code easier; it is also an open-source language. There are many sources to learn python. In this...

3 minutes read.

Python round() function

Python round() function The round() function in Python returns a number rounded to ‘ndigits’ precision after the decimal point. If ndigits is omitted or is None, it returns the nearest integer to its input. Syntax round(number[, ndigits]) Parameter Number: This parameter represents the...

1 minute read.

Python IDE

What is an IDE?  An IDE or an Integrated Development Environment is a type of software where developers can develop many software’s and applications and run the programs inside it. An...

11 minutes read.

Create Table Using PyQt5 in Python

PyQt5: PyQt5 is one of several solutions that Python offers for creating GUI applications. Cross-platform GUI toolkit PyQt5 is a collection of Python interfaces for Qt version 5. With this library's...

4 minutes read.

Python Program to Convert Decimal into Binary, Octal, and Hexadecimal

Python Program to Convert Decimal into Binary, Octal, and Hexadecimal We know that the most widely used number system is a decimal system, but the computer only understands binary values. The...

2 minutes read.