AI Glossary / Data science and analytics / Intermediate
List
Definition
A list is an ordered collection of elements. In programming, the exact operations and implementation depend on the language. In Python, a list is a mutable sequence: elements can be accessed by position, replaced, added, or removed. A Python list can contain repeated values and references to objects of different types. This entry uses Python to make the general concept concrete.
What it means
Order means that each element occupies a position, not that the elements are automatically sorted. A list such as [8, 3, 8] preserves that sequence and includes the value 8 twice. Lists are useful for holding a collection that changes during a program, such as records awaiting processing. They are distinct from sets, which focus on unique members, and dictionaries, which associate keys with values. Choosing a collection type communicates how the program intends to use the data.
How it works
A Python list uses zero-based indexing, so its first element is at position 0. Indexing retrieves one element, while slicing selects a sequence of elements. Methods such as append add an element, and operations can change the existing list. Assigning a list to another name does not copy it: the names can refer to the same object. A shallow copy creates a separate outer list while retaining references to the same contained objects. These reference relationships matter when nested data are later modified.
Three practical examples
- Data preparation: an analyst collects validated records into a list before passing them to another processing step.
- User interfaces: a program stores a sequence of selected options and preserves their chosen order.
- Model evaluation: a script keeps predicted labels in a list aligned with the corresponding evaluation examples.
Think of it this way
Think of a row of numbered compartments that can hold items in sequence. A program can inspect a position, add an item, or rearrange the row. The analogy is limited because a Python list stores references to objects; placing the same object in two positions does not necessarily create two independent copies of it.
Worked example
Start with scores = [8, 3, 8]. The expression scores[0] retrieves 8, and scores[1] retrieves 3. Calling scores.append(5) changes the list to [8, 3, 8, 5]. Now assign review = scores. Both names refer to the same list. If review[1] = 4 is performed, scores also shows [8, 4, 8, 5]. No separate list was created by the assignment.
Next, create backup = scores.copy(). This makes a new outer list. Replacing backup[0] with 99 changes backup to [99, 4, 8, 5] while scores remains [8, 4, 8, 5]. The elements here are integers, so this example does not involve mutable nested objects. If the elements were themselves lists, a shallow copy would still share those inner lists. Finally, sorting scores in place would rearrange the existing list; obtaining a separately sorted result is a different operation. The choice matters when the original order links predictions to source records.
Illustrative case study
In a fictional evaluation script, predictions and record identifiers are stored in separate aligned lists. A developer sorts the prediction list to inspect score values but leaves identifiers in their original order. A later report pairs the reordered predictions with the wrong records. The team changes the inspection process to keep each identifier and prediction together and checks alignment before calculating metrics. The case illustrates why list order can carry meaning even when the individual values are all valid.
Common misconceptions and mistakes
A list is not automatically sorted or free of duplicates. Python list assignment does not produce an independent copy. A shallow copy is not a deep copy of nested objects. Appending a list adds that list as one element, whereas extending a list adds the elements of an iterable individually. In-place methods such as list.sort return None rather than the modified list, so assigning their return value to the original name can lose the intended reference. A list also need not be the most suitable structure for every numerical or lookup task.
Business use
Use lists when sequence and flexible updates fit the workflow. Document whether functions mutate a supplied list or return a new collection. Preserve record alignment when lists hold parallel information, and consider storing related fields together rather than relying on matching positions across multiple collections. For large numerical workloads, evaluate an array-oriented representation appropriate to the operations. For frequent membership checks or key-based access, consider whether a set or dictionary expresses the requirement more directly. Select the structure based on behavior, not only familiarity.
People also ask
Can a list contain the same value twice?
Yes. Duplicates are allowed.
Does ordered mean sorted?
No. It means positions are maintained according to the sequence's current arrangement.
Does assigning one list variable to another copy the contents?
In Python, no. The new name refers to the same list unless a copying operation is used.
Check your understanding
Why does changing review[1] also change what scores displays in the example?
Answer: review and scores refer to the same list object. The element replacement mutates that shared object rather than creating a new list.
Related terms
- Sequence
- Index
- Slice
- Mutable Data Types
- Tuple
- Set
- Dictionary
- Shallow Copy
- Array
- mutable data type
- reference
Learn next
Trace a small list through indexing, appending, assignment, and copying while drawing which names refer to which objects. Compare append with extend using a nested list. Then study how to keep identifiers and values aligned when sorting or filtering records.
More in Data science and analytics
- Latent variable - A latent variable is a variable included in a model but not directly observed in the available data.
- Lift - Lift is a ratio that compares an observed or model-selected outcome with a stated baseline.
- Line chart - A line chart displays values as points connected in an ordered sequence, commonly to show change over time.
- Local outlier factor - Local outlier factor, or LOF, is a method that scores how unusual an observation is relative to the density around its neighbors.
- Logarithm - A logarithm gives the exponent needed to raise a specified base to a positive number. If b raised to y equals x, then log base b of x equals y.
- Long Short-Term Memory - Long Short-Term Memory, usually abbreviated LSTM, is a type of recurrent neural network designed to learn relationships across sequences.
Sources
Python tutorial, lists and data-structure operations: https://docs.python.org/3/tutorial/datastructures.html Python built-in types, mutable sequences and list behavior: https://docs.python.org/3/library/stdtypes.html Python data model, objects and shared references: https://docs.python.org/3/reference/datamodel.html#objects-values-and-types
Previous: Liquid State Machine Network Next: List sample