Python

Lists, Tuples and Dictionaries

The next basic data type is the Python list. A list is an ordered collection of elements, and these elements can be of arbitrary type. Lists are mutable, meaning they can be changed in-place. In python lists are indicated by square brackets [ and ]. Items in a list are indexed starting with 0.

List Basics

In [21]:
 
In [8]:
 
In [31]:
 

You can even have lists of lists:

In [21]:
 

Use multiple indexes for lists within lists

In [21]:
 

Index from the end of the list

In [21]:
 

Append an item to a list

In [ ]:
 

Reference multiple items in a list

In [ ]:
 

Open ended indexes go to the ends of lists

In [ ]:
 

Python tuples

Tuples are similar to lists, except that they are immutable, meaning that they cannot be changed in place. Tuples are indicated by parenthesis ( and ).

In [21]:
 
In [21]:
 
In [21]:
 
In [21]:
 

Python dictionaries

Dictionaries are unordered collections of key-value pairs. Dictionaries can use various types for keys (e.g. strings, ints) so long as the key is immutable. Values are looked up by key. In python dictionaries are indicated by curly braces { }

Create a dictionary

In [21]:
 

Lookup value with key

In [21]:
 

Change a value for a key

In [21]:
 

Get all keys in dictionary

In [21]:
 

Get all values in dictionary

In [21]:
 

Check if key exists

In [21]: