Accessing Collection Items

This is part of “An intro to Coding” series of posts, with content from the Enki app. If you stumbled upon this, you can start from the beginning.

Finding an item in a list is done by searching the whole list, starting from the first item onwards.

This can be quite slow for big lists.

If you need fast lookup for a list item, use an array.

Accessing an item in an array is practically instant because there's no searching involved.

Here's how you'd get the third element from the array users:

💡 The array position starts from 0, not 1. That means that the 3rd element in an array is at position 2.

Compared to lists, dictionaries are non-sequential. This means that a dictionary item doesn't have a position.

What matters is the key. To extract a value from a dictionary you need to reference its key.

Using the same example as previously, let's extract the name:

To extract the value, we first write the name of the dictionary (my_dog), followed by a dot (.) and then the key (name). This is commonly referred to as dot notation.

Depending on the programming language, you can also extract a value using a key between square brackets:

This is usually referred to as bracket notation.

Chat with us to learn more

Continue reading

Our newsletter

Get the best curated articles

No spam!