Python Dictionary Methods

Python Dictionary Methods

Python Dictionary Method 1 : clear()

Working: It used to remove all the elements from dictionary

Syntax:

dict.clear()

Example:

dic = {1: ”rushikesh", 2: ”code"}

dic.clear()

Result:

dic = {}


Python Dictionary Method 2 : copy()

Working: It used to return copy of original dictionary, it did not make any changes in original dictionary.

Syntax:

dict.copy()

Example:

dic = {1: ”rushikesh", 2: ”code"}

new = dic.copy()

Result:

new = {1: ”rushikesh", 2: ”code"}


Python Dictionary Method 3 : fromkeys()

Working: It used to return / create dictionary with key, mapping with specific value.? It take two argument, sequence and value.

Syntax:

dict.fromkeys(sequence, value)

Sequence may be any sequence, and value is used to assign to that sequence.

Example:

seq = ['b', 'c', 'd', 'e', 'f‘]

dict = dict.fromkeys(seq) # None value will be provided to every key

print(dict)

#new_dict = dict.fromkeys(seq, 0) # 0 value will be provided to every key

#print(new_dict)

Result:

{'b': None, 'c': None, 'd': None, 'e': None, 'f': None}


Python Dictionary Method 4 : get()

Working: It used to return value for specific key. If key is not exist, then None is return. It also accept default value, that will display when key is not exist.

Syntax:

dict.get(Key) # return value

#dict.get(Key, Default = None) # Return None, if key did not exist

Example:

dic = {1: ”rushikesh", 2: ”code"}

# value = dic.get(1)

value = dic.get(3) # return None

print(value)

Result:

None


Python Dictionary Method 5 : items()

Working: It used to return item form a dictionary in list form. Item is collection of key and value. Dictionary is the collection of items.

Syntax:

dict.items()

Example:

dict = {1: ”rushikesh", 2: ”code"}

dict_items = dic.items()

print(dict_items)

Result:

dict_items([(1, ‘rushikesh'), (2,’code’)])


Python Dictionary Method 6 : keys()

Working: It is used to return all the keys of dictionary in list form.

Syntax:

dict.keys()

Example:

dict = {1: ”rushikesh", 2: ”code"}

_keys = dict.keys()

print(_keys)

Result:

dict_keys([1, 2])


Python Dictionary Method 7 : pop()

Working: It is used to remove specified item from dictionary.

Syntax:

dict.pop(key, default value) # default value will be return if specified key does not exist

Example:

dict = {1: ”rushikesh", 2: ”code"}

element = dict.pop(2)

print(element)

Result:

code


Python Dictionary Method 8 : popitem()

Working: It is used to remove last item from dictionary.

Syntax:

dict.popitem()

Example:

dict = {1: ”rushikesh", 2: ”code"}

element = dict.popitem()

print(element)

Result:

(2,’code’)


Python Dictionary Method 9 : setdefault()

Working: It used to return value of specified key, if key does not exist then default value will be return, if default value does not provided then None is returned.

Syntax:

dict.setdefault(key, default value) # default value will be None if not provided.

Example:

dict = {1: ”rushikesh", 2: ”code"}

element = dict.setdefault(2)

#element = dict.setdefault(3) # None will return

#element = dict.setdefault(3, ‘rushikesh’) # rushikesh will return

print(element)

Result:

Code


Python Dictionary Method 10 : update()

Working: It is used to update dictionary items. It take argument either key value pair item or dictionary.

Syntax:

dict.update()

Example:

dict = {1: ”rushi", 2: ”code”}

dict1 = {3:’rushikesh’}

dict.update(dict1)

# dict.update({3 : ’rushikesh’})

print(dict)

Result:

{1: ”jafri", 2: ”code”, 3:’rushikesh’}


Python Dictionary Method 11 : values()

Working: It is used to return all the values that associated with keys in dictionary.

Syntax:

dict.values()

Example:

dict = {1: ”rushi", 2: ”code”, 3:”rushikesh”}

print(dict.values)

Result:

dict_values(['rushi', 'code', 'rushikesh'])













要查看或添加评论,请登录

Rushikesh J.的更多文章

  • Linux Operating System

    Linux Operating System

    Linux Operating System ======================= => Linux is a community based OS => Linux is free & Open Source => Linux…

  • Infrastructure

    Infrastructure

    ================= Infrastructure ================= Servers (Machines) Databases Storage Network Security Backup =>…

  • Application Architectural Patterns

    Application Architectural Patterns

    =================================== 1) Monolithic Architecture (Outdated) 2) Microservices Architecture (Trending)…

  • DevOps Introduction

    DevOps Introduction

    =============== What is DevOps =============== DevOps = Development + Operations => DevOps is a culture/process in IT…

    2 条评论
  • Try and Exception in Python

    Try and Exception in Python

    Exception is the error and is an event, which occur when program statements are executing. It disrupts the flow of…

  • Python Array With Examples

    Python Array With Examples

    Array in Python Array in Python Array is the collection of items having same data type with contiguous memory location.…

  • Python Date Object

    Python Date Object

    We can work with time and date with Python. There are two module in Python time and datetime that used to work with…

  • String Formatting WITH Problems and Solution

    String Formatting WITH Problems and Solution

    What is String Formatting? It is the way to insert custom string or variable in a text (string). Using string…

  • SET Different Methods With Examples

    SET Different Methods With Examples

    SET Method : add() Working: This method is used to add element to set. Syntax: set.

  • Python SET Comprehension With Examples

    Python SET Comprehension With Examples

    What is Comprehension? ?We create a sequence from a given sequence is called comprehension. ?Sequence means list…

社区洞察

其他会员也浏览了