Jython collections
Frequently, you will need to create collections of other data items. Jython supports two major collection types. The most basic is the sequence type which is an ordered collection of items. Sequences support several subtypes such as strings, lists, and tuples. The other is the map type. Maps support associative lookup via a key value. You'll learn about both types in this section.
A sequence is an ordered collection of items. All sequences are zero-indexed, which means the first element is element zero (0). Indices are consecutive (that is, 0, 1, 2, 3, ...) to the length (less one) of the sequence. Thus sequences are similar to C and Java arrays.
All sequences support indexing (or subscripting) to
select sub-elements. If x is a sequence then the expression
x[n] selects the nth value of the sequence.
Mutable sequences such as lists support indexing on assignment, which
causes elements to be replaced. For these sequences the expression
x[n] = y replaces the nth element of x with y.
Sequences support an extension of indexing, called
slicing, which selects a range of elements. For example,
x[1:3] selects the second through third elements of
x (the end index is one past the selection). Like indexing,
slicing can be used on assignment to replace multiple elements.
In Jython, a sequence is an abstract concept, in that you do not create sequences directly, only instances of subtypes derived from sequences. Any sequence subtype has all the functions described for sequences.
The many valid forms of slicing are summarized below. Assume x is a sequence containing 10 elements (indexes 0 through 9).
| Sample expression | Resulting action | Comments |
x[1]
| Selects index 1 | Same as indexing |
x[1:2]
| Selects index 1 | The end value is one past the selected value |
x[1:]
| Selects index 1 through 9 | Missing value implies the sequence length |
x[:7]
| Selects index 0 through 6 | Missing value implies zero |
x[:-1]
| Selects index 0 through 8 | Negative indexes are adjusted by the sequence length |
x[-6:-3]
| Selects index 3 through 6 | Reverse ranges are supported |
x[:]
| Selects index 0 through 9 | The whole sequence; This makes a copy of the sequence |
x[:1000]
| Selects index 0 through 9 | A reference off the end of the sequence is the end |
x[-100:]
| Selects index 0 through 9 | A reference off the start of the sequence is the start |
x[::2]
| Selects index 0, 2, 4, 6, 8 | The third value skips over selections |
Jython supports several operations between sequences (x and y), as summarized below:
| Operator | Usage | Example |
x + y
| Join (or concatenate) sequences |
[1,2,3] + [4,5,6] --> [1,2,3,4,5,6] |
i * x
x * i
| Repeat sequence |
[1,2,3] * 3 --> [1,2,3,1,2,3,1,2,3] |
o in x
o not in x
| Contains test |
2 in (1,2,3) --> 1 (true)7 not in (1,2,3) --> 1 (true) |
In addition, several functions can be applied to any sequence (x), as summarized below:
| Function | Usage | Example |
len(x)
| Length (number of elements) of the sequence | len(1,2,3) --> 3 |
min(x)
| Smallest value in the sequence | min(1,2,3) --> 1 |
max(x)
| Largest value in the sequence | max(1,2,3) --> 3 |
As I mentioned earlier, a sequence in Jython is an abstract concept, in that you do not create sequences directly, only instances of subtypes derived from sequences. Any sequence subtype has all the functions described for sequences. There are several sequences subtypes, as follows:
- strings are immutable sequences of characters (see Strings)
- tuples are immutable sequences of any data type (see Tuples)
- ranges are immutable sequences of integers (see Ranges)
- lists are mutable sequences of any data type (see Lists)
A string is an immutable sequence of characters treated
as a value. As such, strings support all of the immutable
sequence functions and operators that result in a new
string. For example, "abcdef"[1:4] is the new
string "bcd". For more information on string
functions see Appendix B: String methods.
Jython does not have a character type. Characters are represented by strings of length one (that is, one character).
Strings literals are defined by the use of single or triple quoting. Strings defined using single quotes cannot span lines while strings using triple quotes can. A string may be enclosed in double quotes (") or single ones ('). A quoting character may contain the other quoting character un-escaped or the quoting character escaped (proceeded by the backslash (\) character). See Appendix A: Escape characters for more on this.
Following are some example strings:
- "This is a string"
- 'This is also a string'
- "This is Barry's string"
- 'Barry wrote "Introduction to Jython"!'
- "This is an escaped quote (\") in a quoted string"
- r"\s*xyx\s*" - equivalent to"\\s*xyx\\s"
- u"the number one is \u0031" (vs. "the number one is \x31")
Note that the next-to-last example shows a raw string. In raw strings the backslash characters are taken literally (that is, there is no need to double the backslash to get a backslash character). This raw form is especially useful for strings rich in escapes, such as regular expressions. We'll talk more about regular expressions in Part 2 of this tutorial.
The last example shows a Unicode string and how to create Unicode escaped values. Note that all strings are stored using Unicode character values (as provided by the JVM); this format just lets you enter Unicode character values.
For convenience, multiple strings separated by only
white space are automatically concatenated (as if the +
operator was present) by the Jython parser. This makes it
easy to enter long strings and to mix quote types in a
single string. For example the sequential literals here:
"This string uses ' and " 'that string uses ".'
becomes this string:
This string uses ' and that string uses ".
Triple quoting is used to enter long strings that include new-lines. Strings defined using single quotes cannot span lines while strings using triple quotes can. They can also be used to enter short (single-line) strings that mix quote types. For example, the following is one long multi-line string:
r"""Strings literals are defined by the use
single or triple quoting.
Strings defined using single quotes cannot span
lines while strings using triple quotes can.
A string may be enclosed in quotes (") or apostrophes (').
They may contain the other character un-escaped
or the quoting character escaped
(proceeded by the backslash (\) character."""
|
While this is a short mixed-quote string: '''This string uses ' and that string uses ".'''
Jython strings supports a special formatting operation
similar to C's printf, but using the modulo (%)
operator. The right-hand set of items is substituted
into the left-hand string at the matching %x locations in the string. The set value is usually a single value, a tuple of values, or a dictionary of values.
The general format of the format specification is:
%{(key)}{width}{.precision}x
|
Here's a guide to the format items:
- key: Optional key to lookup in a supplied dictionary
- width: Minimum width of the field (will be longer for large values)
- precision: Number of digits after any decimal point
- x : Format code as described (in Appendix H: Format codes)
For example
print "%s is %i %s %s than %s!" % ("John", 5, "years", "older", "Mark")
print "Name: %(last)s, %(first)s" % {'first':"Barry", 'last':"Feigenbaum", 'age':18}
|
prints
John is 5 years older than Mark! Name: Feigenbaum, Barry |
Tuples are immutable lists of any type. Once created they cannot be changed. Tuples can be of any length and can contain any type of object. Some examples are shown here:
| Example | Comment(s) |
()
| An empty tuple |
(1,)
| A tuple with one element, an integer; the comma is needed to distinguish the tuple from an expression like (1) |
(1, 'abc', 2, "def")
| A tuple with four elements, two integers and two strings |
((), (1,), (1,2), (1,2,3))
| A tuple of tuples; Each sub-list contains integers |
(1, "hello", ['a','b','c'], "goodbye")
| A mixed tuple of integers, strings and a sub-list of strings |
v1 = 1; v2 = 10
(1, v1, v2, v1 + v2)
| A tuple of integers; variable references and expressions are supported |
Note that although a tuple is immutable, the elements in it may not be. In particular, nested lists (see Lists) and maps (see Maps and dictionaries) can be changed.
To implement iteration (see the The for statement) Jython uses immutable sequences of increasing integers. These sequences are called ranges. Ranges are easily created by two built-in functions:
-
range({start,} end {,inc}) creates a small range.
All elements of the range exist. -
xrange({start,} end {,inc}) creates a large range.
Elements are created only as needed.
Ranges run from start (defaults to 0), up to but not
including end, stepping by inc (defaults to 1). For example:
print range(10) # prints [0,1,2,3,4,5,6,7,8,9] print range(2,20,2) # prints [2,4,6,8,10,12,14,16,18] print range(10,0,-1) # prints [10,9,8,7,6,5,4,3,2,1] |
Lists are mutable sequences of any type. They can grow or shrink in length and elements in the list can be replaced or removed. Lists can be of any length and can contain any type of object. For more information on list functions see Appendix C: List methods. Some examples are shown below.
| Example | Comment(s) |
[]
| An empty list |
[1]
| A list with one element, an integer |
[1, 'abc', 2, "def"]
| A list with four elements, two integers and two strings |
[[],[1],[1,2],[1,2,3]]
| A list of lists; Each sub-list contains integers |
[1, "hello", ['a','b','c'], "goodbye"]
| A mixed list of integers, strings and a sub-list of strings |
v1 = 1; v2 = 10
[1, v1, v2, v1 + v2]
| A list of integers; variable references and expressions are supported |
Lists support the notion of Last-In/First-Out
(LIFO) stacks and First-in/First-out (FIFO) queues.
Using list x to create a stack, remove items with
x.pop() (or the equivalent
x.pop(-1)). Using list x to create a
queue, remove items with x.pop(0). To add
elements to the list use x.append(item).
For example:
l = [1,2,3,4,5] # define a list l.append(6) # l is [1,2,3,4,5,6] w = l.pop() # w is 6, l is [1,2,3,4,5] x = l.pop(-1) # x is 5, l is [1,2,3,4] y = l.pop(0) # y is 1, l is [2,3,4] z = l.pop(0) # z is 2, l is [3,4] |
Lists can also be created via an advanced notation,
called list comprehensions. List comprehensions are
lists combined with for and if
statements to create the elements of the list. For more
information see The for statement and
The if statement. Some example list comprehensions follow:
| Example | Resulting list |
[x for x in range(10)]
| [0,1,2,3,4,5,6,7,8,9] Same as range(10)
|
[x for x in xrange(1000)]
| [0,1,2,..., 997, 998, 999] Same as range(1000)
|
[(x < y) for x in range(3) for y in range(3)]
| [0,1,1,0,0,1,0,0,0] |
[x for x in range(10) if x > 5]
| [6,7,8,9] |
[x ** 2 + 1 for x in range(5)]
| [1,2,5,10,17] |
[x for x in range(10) if x % 2 == 0]
| [0,2,4,6,8] |
Mapping types support a mutable set of key-value pairs (called items). Maps are distinct from sequences although they support many similar operations. They are similar to sequences in that they are abstract; you work only with map subtypes, of which the most commonly used type is the dictionary. For more information on map functions see Appendix D: Map methods.
Maps support associative lookup via the key value. A key
can be any immutable type. Keys must be immutable as they
are hashed (see Appendix E: Built-in functions) and the hash value must stay stable. Common key
types are numbers, strings, and tuples with immutable
elements. Values may be of any type (including
None). If m is a map, function
len(m) returns the number of items in the map.
Maps, like sequences, support subscripting, but by key instead of index. For example, if m is a map, x = m["x"] gets a value from the map and m["x"] = x adds a new value to or replaces a value in the map.
Some example dictionary literals are below:
| Example | Comment(s) |
{}
| An empty dictionary |
{1:"one", 2:"two", 3:"three"}
| A dictionary with three elements that map integers to names |
{"one":1, "two":2, "three":3}
| A dictionary with three elements that map names to integers |
{"first':'Barry", "mi":"A", "last":"Feigenbaum"}
| A dictionary that maps a name |
{"init":(1,2,3), "term":['x','y','z'], "data":{1:10,2:100.5}}
| A dictionary containing a tuple, a list, and another dictionary |
t = (1,2,3); l = ['x','y','z']; d = {1:10,2:100.5}
{"init":t, "term":l, "data":d}
| A dictionary containing a tuple, a list, and another dictionary; variable references and expressions are supported |
As shown in Formatting strings and values, dictionaries are convenient for format mapping.



