Chapter 7Data Structures

Only the simplest programs can get away with storing data in variables. But these next programs will get you to think about storing data in lists or name/value pairs, or even a combination of the two.

Depending on the language you choose, you might be looking for arrays, lists, hashes, hashmaps, dictionaries, associative arrays, or maps. And while languages have different names, the concepts are the same. You group data together using data structures. To keep it simple, I’m going to use the terms array and map.

An array is a data structure that holds a list of values:

 
colors = [​"Red"​, ​"Green"​, ​"Blue"​];

Usually, the order of items in the array is preserved. To get a value out, you access it by its index, which ...

Get Exercises for Programmers now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.