Concepts / Iterating Over Strings and Lists

Iterating Over Strings and Lists

The important question is not only which characters are present, but also how those characters are grouped: one string or multiple list elements.

  • Programming

One Sequence, Two Structures

A string and a list of characters can contain the same visible characters, but they are not the same kind of value. A string remains one string, while a character list contains separate elements. This distinction matters when you iterate over the data or access its elements by index.

Key point

The important question is not only which characters are present, but also how those characters are grouped: one string or multiple list elements.

Tracing the Conversion

The list() function converts a string into a list. During this conversion, each character becomes one element of the resulting list. For the generated example list("hello"), the result contains five elements: h, e, l, l, and o. The two l characters remain separate elements, even though they have the same value.

helloone stringhelement 0eelement 1lelement 2lelement 3oelement 4conversionconversionconversionconversionconversion
What does list("hello") contain, and how does one string become five separate list elements?

Reading Positions

Each character in the resulting list is accessible by an index. Indexing starts at 0, so the first character has index 0, the second has index 1, and so on. In the generated character list from "hello", h is at index 0, e is at index 1, the first l is at index 2, the second l is at index 3, and o is at index 4.

0h1e2l3l4onextnextnextnext
How do the characters in the original string map to positions and indexes in the resulting list?
Worked example

Predicting the List Structure

Determine the elements and indexes in the character list produced by list("cat").

  1. 1
    Separate the characters

    The string contains c, a, and t, so the resulting list has one element for each of those characters.

  2. 2
    Assign indexes

    The first element receives index 0, the second receives index 1, and the third receives index 2.

  3. 3
    Check the structure

    The result is a list of three single-character strings, not one string containing three characters.

Result

The character list contains c at index 0, a at index 1, and t at index 2.

Iteration Output

Both strings and lists are sequences, so they can be traversed. The difference is the structure being traversed. Iterating over the string "cat" encounters characters as part of one string sequence. Iterating over the generated list containing c, a, and t encounters the same character values as separate list elements. The visible character values can match while the surrounding data structure remains different.

catone stringcencountered charactercharacter listthree list elementsclist elementaencountered characteralist elementtencountered charactertlist elementiterationiterationiterationelementelementelement
What does each loop iteration produce when traversing a string versus a list containing the same characters?
Key point

Conversion is useful when you need to reason about the characters as separate list elements. It does not erase the distinction between the original string and the resulting list.

Names That Clarify

Choose a variable name that describes the value and does not interfere with a built-in function. In particular, avoid using list as a variable name because list is also the name of the function used for this conversion. Also avoid a variable name that is only the lowercase letter l, because it can be visually ambiguous and difficult to distinguish from other characters.

Name choiceIssueClearer direction
listCan shadow the built-in function named listUse a name describing the character collection
lVisually ambiguousUse a readable descriptive name
charactersDescribes the stored character collectionSuitable when the value is a character list

Variable names should protect access to built-in functions and make the stored data easier to recognize.

Common Naming and Structure Errors

  • Common mistake

    Treating a string and a character list as the same structure

    What it looks like

    Assuming that a string containing "cat" is already a list with three elements

    Why it fails

    A string and a list of characters are fundamentally different even though both are sequences.

    Do this instead

    Use list() when the characters need to become separate list elements.

  • Common mistake

    Forgetting that conversion creates one element per character

    What it looks like

    Predicting one element containing "hello" after converting the string

    Why it fails

    The list() function converts the string so that each element is a single-character string.

    Do this instead

    Count the characters and place each one in its own list position.

  • Common mistake

    Starting indexes at 1

    What it looks like

    Assigning the first character index 1

    Why it fails

    Indexes for the resulting character list start at 0.

    Do this instead

    Use index 0 for the first character and continue upward from there.

  • Common mistake

    Using list as a variable name

    What it looks like

    Naming the character collection list

    Why it fails

    This shadows the built-in function named list.

    Do this instead

    Choose a descriptive name that does not reuse the built-in function name.

  • Common mistake

    Using the single letter l as a variable name

    What it looks like

    Naming a character collection l

    Why it fails

    The name is visually ambiguous.

    Do this instead

    Use a readable name that communicates the value's purpose.

Prediction Practice

EASY

Predict the character list produced by list("book"). State the value at each index, beginning with index 0. Then explain how that result differs structurally from the original string.

Hints
  • Create one element for each character in the string.
  • Remember that indexing starts at 0.
  • The two o characters occupy separate positions.
EASY

Choose the better variable name for a character list: list, l, or characters. Explain why the other two choices are less suitable.

Hints
  • Check whether a choice conflicts with a built-in function.
  • Check whether the name is visually clear.
  • Prefer a name that describes the stored data.

Key Takeaways

  1. 1
    A string and a list of characters are different structures, even though both are sequences.
  2. 2
    The list() function converts a string into a list whose elements are single-character strings.
  3. 3
    The resulting character list uses indexes beginning at 0.
  4. 4
    Repeated characters occupy separate list elements and separate positions.
  5. 5
    Avoid variable names such as list and l because they can conflict with built-ins or create visual ambiguity.

At a glance

  • A string remains one string, while a character list contains separate single-character string elements.
  • list() converts each character of a string into its own list element.
  • Character-list indexes begin at 0, so the first character is at index 0.
  • Variable names should avoid shadowing list and should not rely on visually ambiguous names such as l.

Related concepts

More concepts starting with I