One variable can refer to a collection that contains many values. The variable is the name your program uses; the collection is the value behind that name. The structure of the collection determines whether you work with items by position, by membership, by key, or in a particular processing order.
How one variable can hold many values
A variable is a name that lets a program refer to a value. That value does not have to be a single number or piece of text: it can itself be a collection. For example, scores = [91, 84, 97] binds the name scores to an ordered collection of three numbers. The collection is one value from the program’s point of view, even though it contains several items.
Collections are not all interchangeable. Some preserve order, some rule out duplicates, and some associate each value with a key. The best choice depends on what your program needs to do with the data.
Common structures and when to use them
| Need | Structure | How it organizes or accesses data |
|---|---|---|
| Keep values in order and refer to them by position | Sequence, such as a Python list | Items form an ordered series; positions let you refer to particular items. |
| Add and remove items at one end, with the newest item handled first | Stack | Last-in, first-out (LIFO): the last item added is the first retrieved. |
| Process items in the order they arrive | Queue | First-in, first-out (FIFO): the first item added is the first retrieved. |
| Keep only unique values or check membership | Set | Duplicate values are not retained as separate set members; set operations include union, intersection, and difference. |
| Look up a value using a meaningful label | Mapping, such as a Python dictionary | Each key is associated with a value, so you can retrieve a value by its key. |
Sequences: when order and position matter
A sequence is a natural fit when the order of items matters or when you want to refer to items by position. In Python, the basic sequence types include lists, tuples, and ranges. A list can be changed after it is created; a tuple is immutable. See Python’s built-in types documentation for the language’s sequence definitions and details.
Outdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware matchWindows Errors? Fix Them Before They Spread
Repair common Windows errors and clear accumulated junk for a smoother, more stable PC - no reinstall needed.Free scan · no reinstall#1 Best Overall
For example, scores = [91, 84, 97] stores the scores in order. A sequence is useful for an ordered set of steps, a row of results, or any collection where position carries meaning. Do not assume every language’s sequence type has the same implementation or performance characteristics.
Sets: when uniqueness matters
A set represents unique values and is useful when you need to test whether an item is present or combine groups using set operations. In Python, seen = {"ada", "lin"} is a set. Python documents sets as unordered collections, so do not rely on the order in which their items appear when iterating. The Python data structures tutorial describes set membership, uniqueness, and operations such as union and intersection.
Rank #2
Mappings: when values need names
A mapping connects keys to values. For example, ages = {"Ada": 36, "Lin": 29} associates each name with an age. In a Python dictionary, keys are unique; in the documented Python version, iterating over a dictionary follows insertion order. Use a mapping when lookup by a meaningful key makes more sense than remembering an item’s position. Python’s tutorial explains dictionaries and their key-value pairs.
Stacks and queues: when processing order matters
Stack: last in, first out
A stack processes the most recently added item first. Python lists work naturally as stacks when items are added and removed at the end with append() and pop(). Python’s tutorial notes that list methods make this use easy: the last element added is the first retrieved.
Rank #3
Queue: first in, first out
A queue processes the earliest arrival first. In Python, the tutorial recommends collections.deque for this job. Removing the first item from a list requires the remaining items to shift, so a list is not efficient for queue operations at the front. A deque is designed for fast appends and pops at both ends. These recommendations describe Python’s documented behavior; they are not a universal speed ranking for every language or implementation. See the Python tutorial’s stack and queue examples.
Choosing a structure for your program
Before choosing, identify the operations your code will perform most often. A structure that fits those operations makes the code’s intent clearer and avoids relying on behavior it does not promise.
Rank #4
- color: White
- INTRODUCTION TO ALGORITHMS, FOURTH EDITION
- Does order matter? Choose a sequence if order and positions matter; do not depend on ordering from an unordered structure.
- Can values repeat? A set represents unique members; use another structure if repeated entries must remain distinct.
- How will you find an item? Use a position for sequence access, membership for set checks, or a key for mapping lookups.
- Where will items be added or removed? A stack handles one-end, newest-first work; a queue handles arrival-order work.
- Must the collection change? Check whether the language’s chosen type is mutable or immutable. Python tuples, for example, are immutable.
- What does your language guarantee? Check its documentation for ordering, supported operations, and relevant performance details instead of assuming similar names mean identical behavior.
These names vary by programming language
The examples above use Python terminology, but the underlying ideas appear in other languages under different names and with language-specific details. JavaScript has arrays, sets, and maps. MDN describes JavaScript arrays as regular objects with integer-keyed properties related to length, and as a good choice for ordered lists; JavaScript also provides typed arrays for array-like views over binary data buffers. JavaScript Set represents unique values, while Map associates keys and values. These types should not be assumed to behave exactly like Python lists, sets, or dictionaries. MDN’s JavaScript data types and data structures guide explains those distinctions.
Where to learn more
If you want to explore beyond these everyday structures, Open Data Structures is a free online resource covering topics including stacks, queues, deques, lists, hash tables, trees, heaps, and graphs, with Java and C++ implementations.
The Tool Desk
Outbyte PC Repair FREERepair Windows errors before they cause bigger problemsFix Now →Outbyte Driver Updater FREEFix the driver behind crashes, sound loss and screen glitchesFind Drivers →Quick Recap
Best Value
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




