October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PCOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk4 min

One Variable, Many Values: Understanding Data Structures

A variable can refer to a collection of values. Learn how sequences, sets, mappings, stacks and queues organize data and when each structure fits.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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
Sale
Data Structures and Algorithms in Python
  • Used Book in Good Condition

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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
Sale
Introduction to Algorithms, fourth edition
  • 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.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Quick Recap

SaleBestseller No. 1
SaleBestseller No. 2
Data Structures and Algorithms in Python
Data Structures and Algorithms in Python
Used Book in Good Condition
$125.13
SaleBestseller No. 4
Introduction to Algorithms, fourth edition
Introduction to Algorithms, fourth edition
color: White; INTRODUCTION TO ALGORITHMS, FOURTH EDITION
$99.47
SaleBestseller No. 5

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.