Python Programming/Arrays

From Wikibooks, the open-content textbooks collection

Jump to: navigation, search
Previous: Basic Math Index Next: Decision Control

Arrays allow you to use multiple variables in an organized and efficient fashion. There are four kinds of arrays in Python: Lists, Tuples, Dictionaries, and Sets (actually there are more, but these are the most commonly used types).

Contents

[edit] Lists

A list is just what it sounds like: a list of variables, organized in order. A list is created using square brackets, and the values of the list are separated by commas. For example:

spam = ["bacon", "eggs", 42]

Lists may contain objects of varying types. It may hold both the strings "eggs" and "bacon" as well as the number 42.

Items in a list can be accessed through its index starting at 0. To call a specific item in a list, you refer to it by the name of the list, followed by the item's number in the list inside brackets. For example:

>>> spam
['bacon', 'eggs', 42]
>>> spam[0]
'bacon'
>>> spam[1]
'eggs'
>>> spam[2]
42

You can also use negative numbers, which count backwards from the end of the list:

>>> spam[-1]
42
>>> spam[-2]
'eggs'
>>> spam[-3]
'bacon'

The len() function also works on arrays, returning the number of items in the array:

>>> len(spam)
3

Note that the len() function counts the number of item inside a list, so the last item in spam (42) has the index (len(spam) - 1).

The items in a list can also be changed, just like the contents of a variable:

>>> spam = ["bacon", "eggs", 42]
>>> spam
['bacon', 'eggs', 42]
>>> spam[1]
'eggs'
>>> spam[1] = "ketchup"
>>> spam
['bacon', 'ketchup', 42]

It is also possible to add items to a list. There are many ways to do it, the easiest way is to use the append() method of list:

>>> spam.append(10)
>>> spam
['bacon', 'eggs', 42, 10]

The append() method would put its argument into the end of the list. If you want to insert an item inside a list at a certain index, you may use the insert() method of list, for example:

>>> spam.insert(1, 'and')
>>> spam
['bacon', 'and', 'eggs', 42, 10]


You can also delete items from a list using the del statement:

>>> spam
['bacon', 'and', 'eggs', 42, 10]
>>> del spam[1]
>>> spam
['bacon', 'eggs', 42, 10]
>>> spam[0]
'bacon'
>>> spam[1]
'eggs'
>>> spam[2]
42
>>> spam[3]
10

As you can see, the list re-orders itself, so there are no gaps in the numbering.

For further explanation on list, see Data Structure/Lists

[edit] Tuples

Tuples are similar to lists, except they are immutable. Once you have set a tuple, there is no way to change it whatsoever: you cannot add, change, or remove elements of a tuple. Otherwise, tuples work identically to lists.

To declare a tuple, you use commas:

unchanging = "rocks", 0, "the universe"

It is often necessary to use parentheses to differentiate between different tuples, such as when doing multiple assignments on the same line:

foo, bar = "rocks", 0, "the universe" # An error: 2 elements on left, 3 on right
foo, bar = "rocks", (0, "the universe")

Unnecessary parenthesis can be used without harm, but nested parentheses denote nested tuples:

>>> var = "me", "you", "us", "them"
>>> var = ("me", "you", "us", "them")

both produce:

>>> print var 
('me', 'you', 'us', 'them')

but:

>>> var = ("me", "you", ("us", "them"))
>>> print(var)
('me', 'you', ('us', 'them')) # A tuple of 3 elements, the last of which is itself a tuple.

For further explanation on tuple, see Data Structure/Tuples

[edit] Dictionaries

Dictionaries are also like lists, and they are mutable -- you can add, change, and remove elements from a dictionary. However, the elements in a dictionary are not bound to numbers, the way a list is. Every element in a dictionary has two parts: a key, and a value. Calling a key of a dictionary returns the value linked to that key. You could consider a list to be a special kind of dictionary, in which the key of every element is a number, in numerical order.

Dictionaries are declared using curly braces, and each element is declared first by its key, then a colon, and then its value. For example:

>>> definitions = {"guava": "a tropical fruit", "python": "a programming language", "the answer": 42}
>>> definitions
{'python': 'a programming language', 'the answer': 42, 'guava': 'a tropical fruit'}
>>> definitions["the answer"]
42
>>> definitions["guava"]
'a tropical fruit'
>>> len(definitions)    
3

Also, adding an element to a dictionary is much simpler: simply declare it as you would a variable.

>>> definitions["new key"] = "new value"
>>> definitions
{'python': 'a programming language', 'the answer': 42, 'guava': 'a tropical fruit', 'new key': 'new value'}

For further explanation on dictionary, see Data Structure/Dictionaries

[edit] Sets

Sets are just like list, except that it is unordered and it does not allow duplicate values. Elements of a set are neither bound to a number (like list and tuple) nor to a key (like dictionary). The reason for using set over other data types is that set is much faster for huge number of items than a list or tuple and sets provide fast data insertion, deletion, and fast membership testing. Sets also support mathematical set operations such as testing for subsets and finding the union or intersection of two sets.

>>> mind = set([42, 'a string', (23, 4)])
>>> mind
set([(23, 4), 42, 'a string'])


>>> mind = set([42, 'a string', 40, 41])
>>> mind
set([40, 41, 42, 'a string'])
>>> mind = set([42, 'a string', 40, 0])
>>> mind
set([40, 0, 42, 'a string'])
>>> mind.add('hello')
>>> mind
set([40, 0, 42, 'a string', 'hello'])

Note that sets are unordered, items you add into sets will end up in an indeterminable position, and it may also change from time to time.

>>> mind.add('duplicate value')
>>> mind.add('duplicate value')
>>> mind
set([0, 'a string', 40, 42, 'hello', 'duplicate value'])

Sets cannot contain a single value more than once. Unlike lists, which can contain anything, the types of data that can be included in sets is restricted. A set can only contain hashable, immutable data types. Integers, strings, and tuples are hashable; lists, dictionaries, and other sets (except frozensets, see below) are not.

[edit] Frozenset

The relationship between frozenset and set is like the relationship between tuple and list. Frozenset is an immutable version of set. An example:

>>> frozen=frozenset(['life','universe','everything'])
>>> frozen
frozenset({'universe', 'life', 'everything'})

[edit] Other data types

Python also has other types of arrays, although these are less frequently used and they need to be imported from the standard library before used. We will only brush on them here.

array
A typed-list, an array may only contain homogeneous values.
collections.defaultdict
A dictionary that, when an element is not found, returns a default value instead of error.
collections.deque
A double ended queue, allows fast manipulation on both sides of the queue.
heapq
A priority queue.
Queue
A thread-safe multi-producer, multi-consumer queue for use with multi-threaded programs. Note that a list can also be used as queue in a single-threaded code.

For further explanation on set, see Data Structure/Sets

[edit] 3rd party data structure

Some useful data types in Python don't come in the standard library. Some of these are very specialized on their use. We'll mention some of the more well known 3rd party types.

numpy.array
useful for heavy number crunching
sorteddict
like the name says, a sorted dictionary
more
this list isn't comprehensive

[edit] Notes


Previous: Basic Math Index Next: Decision Control