Collections

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.

Sometimes a data type is best represented as a collection of one or more data types.

Imagine you're writing a program to model a train.

One way you would represent a train as a data type would be a collection of train car types.

Each train car type can further be a collection of passenger and luggage data types and so on.

Collections don't have to hold the same type.

Think about modeling a shopping bag. It can hold toys, groceries, and clothing for example.

Lists

A List is a data type that represents a sequential collection.

Usually, you can mix elements with different data types in a list, but some languages only let you use one data type in a list.

Lists items commonly have a direction:

Which can also point both ways if needed:

Sometimes we can see lists written using square brackets [ ], with the values separated by commas inside.

The above is a special kind of list called an array.

An array is a list where each item can be accessed based on its position.

Dictionaries

Dictionaries are associative collections that represent key/value pairs.

The name is inspired by our everyday language dictionaries, which are collections of word/definition pairs.

One way to write a dictionary is using curly braces ({ }):

💡 You can leave comments in your code. Usually, single-line comments are marked using //, but some programming languages might use a different way.

In this example, name is the key, and "Artemis" is the value.

Andrei Calabangiu

Content Manager

About Enki

  • AI-powered, customized 1:1 coaching
  • Fully customized to team needs
  • Pay only for active learners and results

More articles