Lists and associations
Paul R. Wellin · Cambridge University Press eBooks · 2015
Lists are the key data structure used in Mathematica to group objects together. They share some features with arrays in other languages such as C and J ava , but they are more general and can be used to represent a wide range of objects: vectors, matrices, tensors, iterator and parameter specifications, and much more. Because lists are so fundamental, an extensive set of built-in functions is available to manipulate them in a variety of ways. In this chapter, we start by looking at the structure and syntax of lists before moving on to constructing, measuring, and testing lists. We then introduce some of the built-in functionality used to manipulate lists such as sorting and partitioning. Finally, we will discuss associations, a feature first introduced in Mathematica 10. Associations provide a framework for efficient representation and lookup of large data structures such as associative arrays (for example, a large database of article and book references or a music library). Many of the things you might wish to do with a list or association can be accomplished using built-in functions and the programming concepts in this book. And most of these operations extend to arbitrary expressions in a fairly natural way, as we will see in later chapters. As such, it is important to have a solid understanding of these functions before going further, since a key to efficient programming in Mathematica is to use the built-in functions whenever possible to manipulate lists and associations as well as general expressions. Creating and displaying lists List structure and syntax The standard input form of a list is a sequence of elements separated by commas and enclosed in curly braces: { e 1 , e 2 , …, e n } Internally, lists are stored in the functional form using the List function with an arbitrary number of arguments.