2

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

dom
  • 23
  • 1
  • 1
  • 4

5 Answers5

4

"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.)

Laurence Gonsalves
  • 131,857
  • 32
  • 232
  • 285
0

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?".

Community
  • 1
  • 1
iTayb
  • 11,765
  • 23
  • 78
  • 132
0

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).

Nicholas Riley
  • 42,081
  • 5
  • 99
  • 124
0

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)]

However you can just use zip/izip for this.

Mike Lewis
  • 61,841
  • 20
  • 140
  • 111
0

It sometimes refers to two lists whose elements are in correspondence. See this SO question.

Community
  • 1
  • 1
jdigital
  • 11,656
  • 4
  • 31
  • 50