Concepts / Iterating Over Strings and Lists

Iterating Over Strings and Lists

A list of characters is fundamentally different from a string, even though both are sequences.

  • 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.

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.

conversionconversionconversionconversionconversionhelloone stringhelement 0eelement 1lelement 2lelement 3oelement 4
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.

nextnextnextnext0h1e2l3l4o
How do the characters in the original string map to positions and indexes in the resulting list?

Predicting the List Structure

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

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

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

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

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.

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

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

Common Naming and Structure Errors

  • Treating a string and a character list as the same structure

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

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

  • Forgetting that conversion creates one element per character

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

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

  • Starting indexes at 1

    Indexes for the resulting character list start at 0.

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

  • Using list as a variable name

    This shadows the built-in function named list.

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

  • Using the single letter l as a variable name

    The name is visually ambiguous.

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

Key Takeaways

  • 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.