Lists, Tuples, Strings, Dictionaries and Built-In Functions Itm 205 Case Mdl 3
An iterator according to the language used in python programming refers to an object that represents various streams of data. According to the functioning of this object, it return the data one element at a time. An example of the interelation is as illustrated below:
>>> L = [1,2,3]
>>> it = iter(L)
>>> print it
<…iterator object at …>
>>> it.next()
1
>>> it.next()
2
>>> it.next()
3
>>> it.next()
Traceback (most recent call last):
File “<stdin>”, line 1, in ?
StopIteration
>>>
The constructor functions mostly known as lists and tuples usually assist in materializing the iterators. An example of this constructor functions is as indicated on the diagram below.
>>> L = [1,2,3]
>>> iterator = iter(L)
>>> t = tuple(iterator)
>>> t
(1, 2, 3)
The sequence unpacking also supports the iterators. For example through the help of the sequence unpacking one can be able to unpack elemnmet N in to a N-turple if there is a high probability that the integrator might return the N-elements. The following illustration shows the how the sequence unpacking operates.
>>> L = [1,2,3]
>>> iterator = iter(L)
>>> a,b,c = iterator
>>> a,b,c
(1, 2, 3)
The most significant features of tiples are that they are immutable. This means that once created, they cannot be changed. The following illustration cleary demonstrates how the tuples operates.
sage: v = (1,2,3,4); v
(1, 2, 3, 4)
sage: type(v)
<type ‘tuple’>
sage: v[1] = 5
Traceback (most recent call last):
…
TypeError: ‘tuple’ object does not support item assignment
Most of the data types that supports the iterators in python programming always can automatically support the creation of an iterator. For example, calling list on a dictionary helps in making the iterator return in order loop on the dictionaries keys. The following illustration demonstrates how most of the data types support the iterators.
>>> m = {‘Jan’: 1, ‘Feb’: 2, ‘Mar’: 3, ‘Apr’: 4, ‘May’: 5, ‘Jun’: 6,
… ‘Jul’: 7, ‘Aug’: 8, ‘Sep’: 9, ‘Oct’: 10, ‘Nov’: 11, ‘Dec’: 12}
>>> for key in m:
… print key, m[key]
Mar 3
Feb 2
Aug 8
Sep 9
Apr 4
Jun 6
May 5
Nov 11
Oct 10
The programmers ought to note that the applying the lists or the tuples on the dictionary always loops over the keys.
Built in functions
The built in functions in the iterators are moer of the tools that help in making the various duplications of list comprehensions within the iterators. However, at the end of the operation the built in functions makes sure that they return the actual lists instead of the iterators. The following illustration demonstrate on how the built in functions are commonly used in the iterators.
map(f, iterA, iterB, …) returns a list containing f(iterA[0], iterB[0]), f(iterA[1], iterB[1]), f(iterA[2], iterB[2]), ….
>>>
>>> def upper(s):
… return s.upper()
>>>
>>> map(upper, [‘sentence’, ‘fragment’])
[‘SENTENCE’, ‘FRAGMENT’]
>>>
>>> [upper(s) for s in [‘sentence’, ‘fragment’]]
[‘SENTENCE’, ‘FRAGMENT’]
The programmers using the python ought to note that there is a function known as the predicament that mostly helps in returning the entire list that contains various sequence that meet a certain condition as illustrates by the program. The following example demonstrates how the predicate ought to take a single value in the iterator.
>>> def is_even(x):
… return (x % 2) == 0
>>>
>>> filter(is_even, range(10))
[0, 2, 4, 6, 8]
This can also be written as a list comprehension:
>>>
>>> [x for x in range(10) if is_even(x)]
[0, 2, 4, 6, 8].
Reference
Hall, T. & Stacey, J.P. (2009). Python 2: For Beginners Only”. New York: Apress.
Lutz M. (2010). Programming Python. New York: O’Reilly Media, Inc.