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 desk3 min

Lists, Tuples, Sets, and Dictionaries in Python: How to Choose

Choose Python’s built-in collections by asking whether you need ordered positions, changeable contents, unique values, or key-based lookup.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use a list for an ordered sequence that may change, a tuple for an ordered sequence whose item references should stay fixed, a set for unique values and set operations, and a dict to associate keys with values. The right choice depends on whether position, changeability, duplicates, or key-based lookup matters.

How the four Python collection types differ

Type Ordering and access Can the collection change? Typical use Constraint
list Ordered; access by integer index or slice Yes A sequence that grows or changes, such as accumulated results Lists are unhashable
tuple Ordered; access by integer index or unpacking No, not at the outer collection level Fixed groups of values, such as coordinates Hashable only if every element is hashable
set Unordered; test membership, but do not index by position Yes; frozenset is the immutable counterpart Unique values, membership checks, and set operations Elements must be hashable
dict Retrieve by key; iteration follows insertion order Yes Associate each key with a value Keys must be hashable and unique

Python’s official data structures tutorial describes sets as unordered collections with no duplicate elements. The language reference documents the underlying mutability, mapping, and ordering rules. These are different tools, not a ranking: select by the behavior your data needs.

When to use a list

A list is a mutable sequence. Write one with square brackets:

items = ["tea", "coffee"]

Lists preserve positions, so use an index or slice to access part of the sequence. Methods such as append change a list in place:

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

items.append("water")

Mutation affects the existing list object. If another variable refers to that same object, changing the list through either name is visible through both. Assigning a list to another name does not, by itself, make a copy.

When to use a tuple

A tuple is an ordered sequence that cannot have its own item references reassigned. It is useful for a fixed group of values, such as a coordinate:

point = (3, 4)
x, y = point

Commas make tuples. A one-item tuple needs a trailing comma: single = (3,). By contrast, (3) is simply the integer 3; parentheses alone can group an expression.

Immutability does not freeze nested objects

A tuple can contain a mutable object. The tuple’s reference to that object cannot be replaced, but the object itself may still change. For example, if a tuple contains a list, that inner list can be modified.

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

This distinction also matters for hashability. A tuple is usable as a dictionary key or set element only when all of its contents are hashable. Attempting to hash a tuple containing an unhashable value, such as a list, raises TypeError. Python’s glossary definition of hashable explains the connection to dictionary keys and set members.

When to use a set

A set holds unique, hashable elements and is useful when duplicates should collapse, when checking whether a value is present, or when comparing groups of values. For example, set(["red", "red", "blue"]) contains one "red" and one "blue".

Sets do not have positional indexing. Their display or iteration order should not be treated as sorted or meaningful. For set values a and b, the operators below express common mathematical operations:

  • a | b: union, values in either set
  • a & b: intersection, values in both
  • a - b: difference, values in a but not b
  • a ^ b: symmetric difference, values in either set but not both

Use set() to create an empty set. Empty braces, {}, create an empty dictionary instead.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

When to use a dictionary

A dictionary maps unique, hashable keys to values. Use it when you want to look something up by a meaningful key rather than by its position:

prices = {"tea": 3, "coffee": 4}
tea_price = prices["tea"]

Assigning a value to a key that already exists replaces that key’s previous value. Dictionaries preserve insertion order when iterated: updating an existing key does not move it, while deleting a key and inserting it again places it at the end. Insertion order has been a language guarantee since Python 3.7, as noted in the Python language reference.

Keys must be hashable. Mutable built-in containers such as lists and dictionaries cannot ordinarily be used as keys; a tuple can be a key only when its elements are hashable too.

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

Quick checks for common mistakes

  • Need an empty set? Write set(), not {}.
  • Need a one-item tuple? Include the comma, as in (item,).
  • Need predictable numeric positions? Choose a list or tuple, not a set.
  • Need a tuple as a key or set member? Check that every nested value is hashable.
  • Need a mutable collection? A list, set, or dictionary can change; a tuple’s own item references cannot.

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
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair 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.