×

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 Set difference() Method

Python Set difference() Method The set.difference() method in Python returns the set difference of two sets(A-B). Syntax set.difference(set1) Parameter set- This argument represents a set (minuend) set1- This arguments represents a set(subtrahend) Return This method returns the difference of the two specified...

2 minutes read.

How to Convert String to List In Python?

How to Convert String to List In Python? We all are familiar with what strings and lists are, let us have a quick revision on them- Strings are a sequence of characters...

4 minutes read.

Decision Tree in Python

Decision Tree is one of the most essential algorithms in the area of machine learning for classification and regression. But let us first talk about the lifespan of every machine learning...

12 minutes read.

Import Function in Python

In Python programming language, the import keyword plays an important role in the whole code because it makes one module make available in another module. Import is used to structure...

3 minutes read.

Python String index() method

Python String index() method The string.index() method in Python finds the lower index of the first occurrence of the specified value or raise a ValueError if the substring is not found. Syntax index(sub[, start[, end]]) Parameter sub:...

2 minutes read.

How to learn python Online

Need to Learn a Programming Language 1. To get a high Paying job We know that Software Engineering is one of the top-paying professions in the world. The top criteria to be...

3 minutes read.

Filter List in Python

Python: Python is one of the most used programming languages, as it is used widely in software and data analysis, web development, etc. It is said to be a user-friendly programming...

3 minutes read.

Reverse a Number in Python

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...

4 minutes read.

Data Class in Python

Introduction In Python 3.7, the dataclass modules are introduced as a practical tool for creating organized classes designed specifically for data storage. These classes contain specific attributes and capabilities to deal...

6 minutes read.

Python String lstrip() method

Python String lstrip() method The string. lstrip () method in Python returns a copy of the string with leading characters removed (based on the string argument passed). Syntax string.lstrip([chars]) Parameter chars(optional): This parameter represents a...

1 minute read.

Python oct() function

Python oct() function The oct() function in Python converts an integer number to an octal string prefixed with “0o”. Syntax oct(x) Parameter x: This parameter represents an Integer Number Return This function returns an octal string. Example 1 #...

1 minute read.

Python Uses

Python has advanced in recent years to rank among the programming languages that are most often used globally. It is utilized in everything, including machine learning, software testing, and website...

4 minutes read.

Python filter() function

Python filter() function The filter() function constructs an iterator from those elements of iterable for which the parameter ‘function’ returns a Boolean value true. Syntax filter(function, iterable) Parameter function: This parameter represents a function to be run for each item in the...

1 minute read.

Anytree Python

Python's anytree module is frequently used within data science-related software. Anytree has a Tolerant License, a Construct File that is public, no errors, no risks, and excellent support. One may...

3 minutes read.

Python Parse Text File

We will learn different ways of read text records in Python. TL;DR The accompanying tells the best way to read all texts from the readme.txt document into a string: with open('readme.txt') as f: lines...

5 minutes read.

Python Logical Operators

In python, there are many operators which we use in our program. Operators are used in manipulating a particular value or operand. Logical operators are used mainly to evaluate an...

7 minutes read.

Python Read Excel file

Python Read Excel file Excel is the spreadsheet application for Window, which is developed by Microsoft. The Excel stores data in the tabular form. It provides easy access to analyze and maintain the...

3 minutes read.

Python: SetBitmap() function in wxPython

SetBitmap() function In the last tutorial, we have discussed about the GetLabelText() function of wx.MenuItem class which is one of the important function of this class. Now, in this tutorial, we...

6 minutes read.

How To Install Python In Ubuntu

How To Install Python In Ubuntu Ubuntu is free and open-source software and it is an essential part of the Linux distribution. It is a popular operating system developed by Canonical. If we...

3 minutes read.

Method Overloading in Python

Overloading is a capability of a method and operator to behave differently according to the nature of parameters. Overloading is popular because it provides a good number of advantages: Reusability of code.Reduces...

3 minutes read.