Computer Science, asked by ayushdhakal430, 2 months ago

Consider the following dictionary:

stock={"Garments":350,"Toys":2000,"Snacks":450,"Cosmetic":1200}

Write a Python statement by calling functions related to dictionary for each

question given below:

1)To display total elements in a dictionary

2)To display list of values​

Answers

Answered by anindyaadhikari13
2

Answer:

Given Dictionary:

→ stock={"Garments":350,"Toys":2000,"Snacks":450,"Cosmetic":1200}

1. To display the total elements in dictionary:

→ print(len(stock))

Output:

→ 4

2. To Display the list of values:

→ list = list(stock.values())

→ print(list)

Output:

→ [350, 2000, 450, 1200]

Explanation:

  • The len function can be used to count the total number of elements present in list, tuples, dictionaries and so on. To calculate the number of elements present, I have used len function.
  • dict.values() returns the values present in the dictionary. Using list() function, it is converted to a list. At last, the list is displayed.
Similar questions