i have just started using python and i can't figure out what is meant by parallel list any info would be great . i think it is just using two list to store info
-
8Where did you come across the term? – nmichaels Apr 05 '11 at 22:08
-
my lecture , ive search the net but can't find anything – dom Apr 05 '11 at 22:14
-
1ask your teacher. This is not a common term in Python programming. – Fred Foo Apr 05 '11 at 22:19
-
cheers for all the replies thats its sorted now thanks again – dom Apr 05 '11 at 22:52
6 Answers
"Parallel lists" is a variation on the term "parallel array". The idea is that instead of having a single array/list/collection of records (objects with attributes, in Python terminology) you have a separate array/list/collection for each field of a conceptual record.
For example, you could have Person records with a name, age, and occupation:
people = [
Person(name='Bob', age=38, occupation=PROFESSIONAL_WEASEL_TRAINER),
Person(name='Douglas', age=42, occupation=WRITER),
# etc.
]
or you could have "parallel lists" for each attribute:
names = ['Bob', 'Douglas', ...]
ages = [38, 42, ...]
occupations = [PROFESSIONAL_WEASEL_TRAINER, WRITER, ...]
Both of these approaches store the same information, but depending on what you're doing one may be more efficient to deal with than the other. Using a parallel collection can also be handy if you want to sort of "annotate" a given collection without actually modifying the original.
(Parallel arrays were also really common in languages that didn't support proper records but which did support arrays, like many versions of BASIC for 8-bit machines.)

- 137,896
- 35
- 246
- 299
The term 'parallel lists' doesn't exist. Maybe you're talking about iterating through two lists in parallel. Then it means you iterate both in the same time. For more read "how can I iterate through two lists in parallel in Python?".
If you're trying to iterate over corresponding items in two or more lists, see itertools.izip
(or just use the zip
builtin if you're using Python 3).

- 43,532
- 6
- 101
- 124
The only time I've seen this term in use was when I was using Haskell. See here:
http://www.haskell.org/ghc/docs/5.00/set/parallel-list-comprehensions.html
Essentially the python equivalent is:
[(x,y) for x in range(1,3) for y in range(1,3)]

- 63,433
- 20
- 141
- 111
Parallel list involve having two different lists, presumably of the same length, which hold matching information based upon their position. For example, index 0 of the first list and index 0 of the second list hold different information for the same conceptual object.
An advantage in python is that one can quite easily use the zip function to iterate over multiple lists at the same time.
A disadvantage, as if with the zip function, is if the two lists are not the same length, you could experience a silent failure. Also, if one list gets sorted and the other doesn't, boom the lists make no sense anymore when considered in tandem. So they could be a bit fragile to use, if you do not take the utmost care.
There are other approaches to store this type of data. First of all, why not use a class? Or perhaps a tuple?
There are times when parallel lists are most natural, but I think in many cases parallel list could be the wrong approach when really the same data structure could be implemented in a more pythonic manner.

- 11
- 3