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

 
 
 

You can even have lists of lists:

 

Use multiple indexes for lists within lists

 

Index from the end of the list

 

Append an item to a list

 

Reference multiple items in a list

 

Open ended indexes go to the ends of lists

 

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 ).

 
 
 
 

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

 

Lookup value with key

 

Change a value for a key

 

Get all keys in dictionary

 

Get all values in dictionary

 

Check if key exists