FAQ: Iterables and Iterators - Finite Iterator: Chain

This community-built FAQ covers the “Finite Iterator: Chain” exercise from the lesson “Iterables and Iterators”.

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

FAQs on the exercise Finite Iterator: Chain

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 #get-help.

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

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

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 #community:Codecademy-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!

import itertools

odd = [5, 7, 9]
even = {6, 8, 10}

all_numbers = list(itertools.chain(odd, even))
print(all_numbers)

  • Print the result which will be:
[5, 7, 9, 8, 10, 6]

I’m probably missing something obvious here, but why does the 6 appear at the end of the list and not at index position 3? I know it must be to do with the different data types of “odd” and “even” but still don’t see why it would put the first value of “even” at the end of the returned list.

1 Like

Yes you’ve got the right idea, it’s the because sets aren’t ordered. Unlike dictionaries which have been insertion ordered since 3.7, you should never rely on the order of a set.

In CPython at least the hashing function used may give you the illusion of order when you iterate through a set (the implementation relies on a hash function which means there is an order, technically). It’s bad form to rely on this though and the bits used for hashing even change depending on the length of the set and all sorts of clever stuff. It’s all very interesting but it’s the kind of implementation detail you should not be basing your program on :slightly_smiling_face:. If you need the output of the set to be ordered then create a new ordered type before using it further.

1 Like