FAQ: Data Structures - Access by Index

This community-built FAQ covers the “Access by Index” exercise from the lesson “Data Structures”.

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

Learn Ruby

FAQs on the exercise Access by Index

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!

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!

I’m a little confused with the instructions, please explain!

1.

Use square bracket notation to print the third value of demo_array to the console.

Remember that the third value is at index 2 , not at index 3 . We start counting indices from zero.

demo_array = [100, 200, 300, 400, 500]

print demo_array [2] <------ my result

Your result is valid, but the SCT may not expect the space.

2 Likes

Your code should print: 300
The demo_array contains 5 elements or values. Arrays allow us to access each element by its index or address if you’d like to think of it that way. The value at index 0 is 100. The value at index 4 is 500. We access the values at each index by using the name of the array followed by the index inside of square brackets like so: demo_array[2]. Hope this helps!

Thank you so much! now i understand

1 Like

The SCT will say your code is incorrect if you have a space between the name of the array and the Index number.

demo_array [2] 300 is printed. wrong!

demo_array[2] 300 is printed. correct!

1 Like