Home > Mobile >  How to define a function without using .values() to get values from a dictionary
How to define a function without using .values() to get values from a dictionary

Time:02-23

def getValues(myDict):
  return list(myDict.items())[0][1] # <-- my code returns only 1 value, I need all values

courses = {'CPS141':60, 'CSS210':30, 'CPS161':20}
print(getValues(courses))

expected output: [60,30,20]

CodePudding user response:

You could use a list comprehension:

[value for key, value in myDict.items()]
  • Related