Understanding Lists as Sequences
A list of characters is fundamentally different from a string, even though both are sequences.
One Text Value, Many Elements
A string and a list of characters are both sequences, but they are different sequence types. A string treats its contents as one text value. A list contains separate elements, so a list made from a string can hold one character in each position. The operation you choose depends on whether you want individual characters, larger segments such as words, or one string rebuilt from several string elements.
What do you think happens?
You have the string "code". Which result matches converting it with list()?
Reveal answer
A list containing "c", "o", "d", and "e"
The list function breaks a string into individual characters. It does not divide the string into words or preserve the whole string as one list element.
Character-Level Conversion
Use list() when the processing goal is to separate every character in a string. For example, applying list() to the string "lamp" produces a list whose elements are "l", "a", "m", and "p". Each character becomes its own list element, rather than the entire string becoming one element.
Turning a Label into Character Elements
Convert the string "sun" into a list of individual characters.
- 1Start with the string
The original value is the single string "sun".
- 2Apply list()
The list function separates the string into its individual characters.
- 3Inspect the elements
The resulting list contains "s", "u", and "n" as separate elements.
["s", "u", "n"]
Words and Delimited Segments
Use split() when a string should be broken into words or larger segments. The delimiter identifies where the string is cut. A delimiter can be supplied when the boundary is specific, such as a comma or another separator. This differs from list(), which treats every character as a separate resulting element.
Separating Comma-Delimited Values
Break the string "tea,coffee,water" into separate segments using the comma as the delimiter.
- 1Identify the delimiter
The comma marks each boundary between neighboring values.
- 2Apply split()
Calling
split()with the comma separates the original string at each comma. - 3Read the resulting list
The list contains "tea", "coffee", and "water" as separate string elements.
["tea", "coffee", "water"]
Rebuilding One String
Use join() when you have a list of strings and need one combined string. The separator string is placed between the list elements. This method has an important asymmetry: join is called on the separator string, not on the list. For example, a separator such as a space can join a list of words into one sentence, while a comma can join values into a comma-separated string.
Joining Words with Spaces
Combine the list ["Lists", "are", "sequences"] into one string with a space between each element.
- 1Choose the separator
Use a single space as the separator because the desired result should have spaces between words.
- 2Call join on the separator
The separator performs the joining operation; the list supplies the string elements to combine.
- 3Inspect the result
The elements become the single string "Lists are sequences".
"Lists are sequences"
Selecting the Right Transformation
Choose the operation by asking what the next stage of processing needs. If every character must become an element, use list(). If words or delimiter-defined segments must become elements, use split(). If separate string elements must become one text value, use join(). These operations describe different directions and different units of separation, so they are not interchangeable.
| Processing goal | Operation | Resulting structure |
|---|---|---|
| Separate every character | list() | A list with one character per element |
| Separate words or delimiter-defined segments | split() | A list with one word or segment per element |
| Combine string elements | join() | One string with the chosen separator between elements |
Use the desired unit of processing to select the operation.
Common Conversion Mistakes
Common mistake Using list() when the goal is to obtain words or larger segments
What it looks likeApplying list() to "red blue" when the desired elements are "red" and "blue"
Why it failslist()separates individual characters, not words.Do this insteadUse split() when the string must be divided into words or delimiter-defined segments.
Common mistake Using split() when every character should become an element
What it looks likeTrying to use a word or segment delimiter to produce one list element for each character
Why it failssplit()divides at delimiters, so it is designed for words or larger segments rather than character-by-character separation.Do this insteadUse list() for individual characters.Common mistake Calling join() on the list
What it looks likeTreating the list as the object that supplies the separator
Why it failsThe join operation is called on the separator string, not on the list.
Do this insteadChoose the separator string first, then use it to join the list of strings.
Common mistake Forgetting that join() combines strings into one string
What it looks likeExpecting join() to produce another list of segments
Why it failsjoin()performs the reverse type of transformation: it combines list elements into a single string.Do this insteadUse join() when the desired final structure is one string.
Practice the Choice
For each goal, choose list(), split(), or join(), then explain what the resulting structure should be: separate "planet" into individual characters; break "north|east|west" into segments at the vertical bar; combine ["high", "speed"] into one string with a hyphen between the elements.
Hints
- Ask whether the desired elements are characters or larger segments.
- For the third goal, identify the separator before choosing the operation.
- Remember that
join()is called on the separator string.
- 1A string and a list of characters are both sequences, but they organize text differently. Use
list()to create one list element per character. Usesplit()to create list elements from words or delimiter-defined segments. Usejoin()to combine a list of strings into one string, remembering that the separator string calls join. Selecting the operation begins with identifying whether you need characters, segments, or a rebuilt string.
Key Takeaways
list()converts a string into a list of individual characters.split()breaks a string into words or segments at a delimiter.join()combines a list of strings into one string with a separator between elements.- The separator string calls
join(); the list does not. - Choose the operation based on whether your goal is character separation, segment extraction, or string recombination.