FAQ: Collections - DefaultDict

This community-built FAQ covers the “DefaultDict” exercise from the lesson “Collections”.

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

Learn Intermediate Python 3

FAQs on the exercise DefaultDict

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

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

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in Language Help.

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

Need broader help or resources? Head to Language Help and Tips and Resources. If you are wanting feedback or inspiration for a project, check out Projects.

Looking for motivation to keep learning? Join our wider discussions in Community

Learn more about how to use this guide.

Found a bug? Report it online, or post in Bug Reporting

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!

Hi there.
It is not clear, why DefaultDict class should be used instead of simple method .get(key, defvalue)?

#get-help In the solution,

site_locations[item] = validated_locations[item]

is used. But I don’t understand why site_locations.update((item, validated_location[item])) does not work.

Could anyone please explain this?

Thank you

Firstly, there seems to be a typo (unless it is just a slip during copy/pasting in the forums). You wrote
validated_location[item] instead of validated_locations[item].

Other than that, have a look at the documentation for update,

In your attempted solution,

site_locations.update((item, validated_locations[item])) 

the outer pair of parentheses belong to the update method, while the inner pair of parentheses belong to the iterable(tuple). But, the documentation mentions “… or an iterable of key/value pairs (as tuples or other iterables of length two)…”

The elements of your iterable(tuple) are not key/value pairs or other iterables which can be interpreted as being key/value pairs.

Here are some variations which should work,


site_locations.update([(item, validated_locations[item])]) 
# The iterable is a list with a single element.
# That element is a tuple/iterable of length 2.

site_locations.update(((item, validated_locations[item]),)) 
# The iterable is a tuple with a single element.
# That element is a tuple/iterable of length 2.
# The comma has been used because there is only one element in the outer tuple and
# we want the parentheses to be interpreted as belonging to a tuple.
# e.g. 
# Single element with parentheses
x = (3)
print(type(x)) # <class 'int'>

x = (3,)
print(type(x)) # <class 'tuple'>

# No need for trailing comma if more than one element is inside parentheses.
# No ambiguity in the parentheses being interpreted as a tuple.
x = (3, 4)
print(type(x)) # <class 'tuple'>

You can also have multiple tuples/iterables of length 2 inside the iterable e.g.

x = {'a': 1, 'b': 2, 'c': 3}

x.update([("a", 3), ("b", 100), ["z", 88]])
# Iterable is a list whose elements are iterables of length 2

print(x)  
# {'a': 3, 'b': 100, 'c': 3, 'z': 88}

# This will also work
x.update((("a", 3), ("b", 100), ["z", 88]))
# Iterable is a tuple whose elements are iterables of length 2
2 Likes