FAQ: Collections - OrderedDict

This community-built FAQ covers the “OrderedDict” 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 OrderedDict

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!

https://www.codecademy.com/courses/learn-intermediate-python-3/lessons/collections-python/exercises/ordereddict-python

Can anyone help me understand this, I think I’ve done this exercise correctly however the hint for step 2 says
" In order to loop through the OrderedDict while keeping track of the index, you can use a counter variable or you can enumerate the items in orders. This looks like: for i, (k, v) in enumerate(orders.items()):. Remember to break once you reach far enough. Also, you can modify the OrderedDict by setting the value after accessing it using a key: orders[key_value] = 'completed purchase'"
But I don’t understadn why we need to keep track of the index, use the ennumerate method, or break?

I’ll include the code I wrote in the codebyte, it passes and lets me move on, I just feel like maybe I’m missing a point the exercise is trying to make.

from collections import OrderedDict # The first 15 orders are provided order_data = [['Order: 1', 'purchased'], ['Order: 2', 'purchased'], ['Order: 3', 'purchased'], ['Order: 4', 'returned'], ['Order: 5', 'purchased'], ['Order: 6', 'canceled'], ['Order: 7', 'returned'], ['Order: 8', 'purchased'], ['Order: 9', 'returned'], ['Order: 10', 'canceled'], ['Order: 11', 'purchased'], ['Order: 12', 'returned'], ['Order: 13', 'purchased'], ['Order: 14', 'canceled'], ['Order: 15', 'purchased']] # Write your code below! orders = OrderedDict(order_data) to_move = [] to_remove = [] for k, v in orders.items(): if v == 'returned': to_move.append(k) elif v == 'canceled': to_remove.append(k) for order in to_remove: orders.pop(order) for order in to_move: orders.move_to_end(order) print(orders)