Math, asked by brainlyriya42, 8 months ago

Explain Dictionary method in Phytons. ​

Answers

Answered by 0praveen0kumar
10

Answer:

Python Dictionary Methods.

Method Description

fromkeys() Returns a dictionary with the specified keys and value

get() Returns the value of the specified key

items() Returns a list containing a tuple for each key value pair

keys() Returns a list containing the dictionary's keys

Answered by AdorableMe
71

Dictionary Methods

The dict class provides various methods using which we can perform a  variety of operations. In addition to these methods, we can use built-in  len() functions to get the length of a dictionary.

In []: len(ticker)

Out[]: 5

In []: len(tickers)

Out[]: 2

◘ Some of the popular methods provided by the dict class :

• items() : This method returns an object containing all items in the  calling object. That is, it returns a list of tuples containing keys and  values.

In []: ticker.items()

Out[]: dict_items([('symbol', 'AAPL'), ('price', 226),

('company', 'Apple Inc'),

('founded', 1976),

('products', ['Machintosh', 'iPod',

'iPhone', 'iPad']])

____________________

• keys() : This method returns all keys in the calling dictionary.

In []: ticker.keys()

Out[]: dict_keys(['symbol', 'price', 'company', 'founded',

'products'])

____________________

• values() : This method returns all values in the calling object.

In []: ticker.values()

Out[]: dict_values(['AAPL', 224.95, 'Apple Inc', 1976,

['Machintosh', 'iPod', 'iPhone',

'iPad']])

____________________

• pop() : This method pops the item whose key is given as an argument.

In []: tickers

Out[]:

{'GOOG': 'Alphabet Inc.',

'AAPL': 'Apple Inc.',

'MSFT': 'Microsoft Corporation'}

In []: tickers.pop('GOOG')

Out[]: 'Alphabet Inc.'

In []: tickers

Out[]: {'AAPL': 'Apple Inc.',

'MSFT': 'Microsoft Corporation'}

____________________

• copy() : As the name suggests, this method copies the calling dictionary to another dictionary.

In []: aapl = ticker.copy()

In []: aapl

Out[]:

{'symbol': 'AAPL',

'price': 224.95,

'company': 'Apple Inc',

'founded': 1976,

'products': ['Machintosh', 'iPod', 'iPhone', 'iPad']}

____________________

• clear() : This method empties the calling dictionary.

In []: ticker.clear()

In []: ticker

Out[]: {}

____________________

• update() : This method allows to add new key-pair value from another dictionary.

In []: ticker1 = {'NFLX' : 'Netflix'}

In []: ticker2 = {'AMZN' : 'Amazon'}

In []: new_tickers = {}

In []: new_tickers.update(ticker1)

In []: new_tickers.update(ticker2)

In []: new_tickers

Out[]: {'NFLX': 'Netflix', 'AMZN': 'Amazon'}b

Similar questions