Chapter III.3. Collections and Dictionaries

An array can be handy when you need to store the same type of information, such as a group of integers. However, if you need to store different information, such as both integers and strings, and you aren't sure how many items you need to store, you probably can't use an array. Instead, you can use a collection or a dictionary.

A collection acts like a resizable array that can hold different data types at the same time while identifying each chunk of data with a number. A dictionary acts like a collection that identifies each chunk of data with a unique key.

The purpose of both collections and dictionaries is to make it easier to store different types of data and retrieve them again with the size and single data type restrictions of an array.

Using a Collection

A collection acts like a super array that can grow and expand without requiring any special commands. In addition, a collection can store different data types (such as integers or strings) or even other data structures, such as an array.

Note

Not all programming languages offer the collection data structure:

  • In some programming languages (like Python and Smalltalk), collections are a built-in feature of the language.

  • In other languages (like C or Pascal), you have to use more primitive data structures (like arrays) to mimic the features of a collection.

  • In many newer languages (like C# and Visual Basic.NET), someone else has already created a collection out of more primitive data structures, ...

Get Beginning Programming ALL-IN-ONE DESK REFERENCE FOR DUMMIES® now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.