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
5 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.)
- 131,857
- 32
- 232
- 285
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).
- 42,081
- 5
- 99
- 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)]
- 61,841
- 20
- 140
- 111