DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run Scan×
Skip to content
World desk1 min

How to Get the First Element of a Tuple in Python

Use my_tuple[0] to retrieve the first item of a nonempty Python tuple, and check for an empty tuple before indexing.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use index 0 to get the first item of a nonempty tuple: first = my_tuple[0]. Python sequence indexes start at zero, so the first item is at index zero.

Get the first item with index zero

For a nonempty tuple, use square brackets with 0:

my_tuple = ("apple", "banana", "cherry")
first = my_tuple[0]
print(first)  # apple

Python’s tutorial shows the same approach with t[0]. Positive sequence indexes are zero-based: index 0 refers to the first item, and index 1 to the second. See the Python tutorial on data structures and the Python Programming FAQ.

Handle an empty tuple

An empty tuple has no first item. Trying empty[0] raises IndexError: tuple index out of range. If the tuple might be empty, check it before indexing:

if my_tuple:
    first = my_tuple[0]
else:
    first = None  # Choose a fallback that suits your program

None is only an example fallback; your program may need another value or a different way to handle missing input.

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

Use unpacking when you need multiple values

Indexing is the clearest choice when you only need the first item. If you want to bind the first two items to names, ordinary unpacking works when the tuple has exactly two items:

coordinates = (12, 8)
x, y = coordinates

To bind the first item and collect any remaining items, use extended unpacking:

first, *rest = my_tuple

Unpacking requires the values to fit the target pattern; extended unpacking captures the remaining values in a list. The tutorial covers tuple unpacking and sequence operations.

Make a one-item tuple correctly

A comma distinguishes a one-item tuple from a parenthesized expression:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
one_item = ("apple",)
first = one_item[0]

("apple") is just the string in parentheses, not a tuple.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Use a named field when it is clearer

If a tuple is a named tuple with descriptive fields, accessing the field by attribute can make the code easier to understand than using a numeric position. Python’s tutorial notes attribute access for named tuples in its discussion of tuples and sequences.

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 *

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.

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
Crashes, No Sound, or Screen Glitches?Free driver scan
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.