Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is meant by parallel lists in Python

Tags:

python

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

like image 530
dom Avatar asked Dec 21 '25 07:12

dom


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

like image 156
Laurence Gonsalves Avatar answered Dec 23 '25 21:12

Laurence Gonsalves