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.
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.
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.
Predicting the List Structure
Determine the elements and indexes in the character list produced by list("cat").
- 1Separate the characters
The string contains c, a, and t, so the resulting list has one element for each of those characters.
- 2Assign indexes
The first element receives index 0, the second receives index 1, and the third receives index 2.
- 3Check 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.
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 choice | Issue | Clearer direction |
|---|---|---|
| list | Can shadow the built-in function named list | Use a name describing the character collection |
| l | Visually ambiguous | Use a readable descriptive name |
| characters | Describes the stored character collection | Suitable 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 likeAssuming that a string containing "cat" is already a list with three elements
Why it failsA string and a list of characters are fundamentally different even though both are sequences.
Do this insteadUse list() when the characters need to become separate list elements.
Common mistake Forgetting that conversion creates one element per character
What it looks likePredicting one element containing "hello" after converting the string
Why it failsThe
list()function converts the string so that each element is a single-character string.Do this insteadCount the characters and place each one in its own list position.
Common mistake Starting indexes at 1
What it looks likeAssigning the first character index 1
Why it failsIndexes for the resulting character list start at 0.
Do this insteadUse index 0 for the first character and continue upward from there.
Common mistake Using list as a variable name
What it looks likeNaming the character collection list
Why it failsThis shadows the built-in function named list.
Do this insteadChoose 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 likeNaming a character collection l
Why it failsThe name is visually ambiguous.
Do this insteadUse a readable name that communicates the value's purpose.
Prediction Practice
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.
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
- 1A string and a list of characters are different structures, even though both are sequences.
- 2The
list()function converts a string into a list whose elements are single-character strings. - 3The resulting character list uses indexes beginning at 0.
- 4Repeated characters occupy separate list elements and separate positions.
- 5Avoid 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.