FAQ: Using Dictionaries - Get All Values

This community-built FAQ covers the “Get All Values” exercise from the lesson “Using Dictionaries”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science

FAQs on the exercise Get All Values

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

In this exercise we are asked to iterate through the values in the num_exercises list and add each value to the total_exercises variable. This can be done with a for loop in the following code:

for x in num_exercises.values():
  total_exercises += x

Lately I have been practicing turning every FOR loop into an equivalent list comprehension. How can we, if at all, express this as a list comprehension? If not, why not?

2 Likes

Hi @skenny999,

You can use a list comprehension for this problem, but you’ll need a little more than just that, because the list comprehension will give you a list, while you need a sum.

This will add up the values:

print(sum([num_exercises[key] for key in num_exercises]))
5 Likes

Looking at the num_exercises dictionary, I wondered what would happen if I replaced one of the values, being all int. type at the moment, to a boolean variable.
In the following link, it has been implied that a boolean might be a value in a dict., though not explicitly written.
https://www.codecademy.com/courses/learn-python-3/lessons/dictionaries-introduction/exercises/make-a-dictionary

In the first place I though that an error might arise, however no error came up.
Moreover, what I unexpectedly recalled by completing the rest of the lesson, it that the ‘True’ boolean variable when used in calculations , takes the value 1. It is like it’s changed from a bool. to an int. variable equal to 1.

num_exercises = {"functions": True, "syntax": 13, "control flow": 15, "loops": 22, "lists": 19, "classes": 18, "dictionaries": 18}

total_exercises = 0

for exercises in num_exercises.values():
  total_exercises += exercises
  print(total_exercises)

2 Likes

I did this way…and it worked. Btw, other methods are more good.

total_exercises = 0
for x in num_exercises:
  total_exercises+=num_exercises[x]

print(total_exercises)
1 Like

Is there anything wrong with doing it in one line like so?
total_exercises = sum(list(num_exercises.values()))

Nothing at all - in fact you can shorten it to total_exercises = sum(num_exercises.values()) - which IMO is the best way to tackle this problem.

3 Likes

for exercises in num_exercises.values():
total_exercises += exercises
print(total_exercises)

Q. Why is it - total_exercises += exercises? And not - exercises += totat_exercises??
I appreciate the first way is correct, I’m just trying to understand it. Thanks

Hello, I know you posted this 3 years ago, but I found your code and observation pretty interesting. Thanks for sharing.

Because total_exercises is a variable (which should be defined in your code before the for loop) and can be assigned any value (in your code the values from the dictionary num_exercises) But exercises is a value itslef, extracted from the dictionary num_exercises, and you can not assign any value to it.

in the topic “Get All Values” it is stated:

There is no built-in function to get all of the values as a list, but if you really want to, you can use:

list(test_scores.values())

but we can use .values() method like .key() to get all values as a list. No need to use list() function!

The keys() and values() methods don’t return lists. As the exercise mentions, if we just want to iterate over the keys or values of a dictionary, then the objects returned by these methods are sufficient and we don’t need to convert them to lists. But, that doesn’t mean that they actually are lists. If you want to use list methods (such as sort/append/pop) on the objects returned by the keys() and values() methods, then we need to convert the objects into lists.

d = {"a": 44, "d": 455, "z": 18, "b": 0, "n": 19, "f": 100}

x = d.keys()     # dict_keys object
y = d.values()  # dict_values object

# Can iterate over the objects without having to convert them into lists
for value in y:
    print(value)

# And also do some other things without having to convert to lists
for value in reversed(y):
    print(value)

print(len(y)) # 6
print(19 in y) # True

# But we can't append/sort/pop these objects
y.append(11)  # AttributeError
y.pop() # AttributeError
y.sort() # AttributeError

print(y) 
# dict_values([44, 455, 18, 0, 19, 100])
y = list(y) # [44, 455, 18, 0, 19, 100]
y.append(11)  # [44, 455, 18, 0, 19, 100, 11]
y.pop() # [44, 455, 18, 0, 19, 100]
y.sort() # [0, 18, 19, 44, 100, 455]

Furthermore, the objects returned by the keys() and values() methods

provide a dynamic view on the dictionary’s entries, which means that when the dictionary changes, the view reflects these changes. (documentation)

d = {"a": 44, "d": 455, "z": 18, "b": 0, "n": 19, "f": 100}

x = d.keys()   

print(x) 
# dict_keys(['a', 'd', 'z', 'b', 'n', 'f'])

del d["z"]
d["x"] = 999

print(x) 
# dict_keys(['a', 'd', 'b', 'n', 'f', 'x'])

# Even though, we didn't invoke the .keys() method again, 
# the object is dynamic and reflects changes made to dictionary.
2 Likes