Concepts / Understanding Lists as Sequences

Understanding Lists as Sequences

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

  • Programming

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()?

  • A list containing "code" as one element

  • A list containing "c", "o", "d", and "e"

  • One string with the characters separated by spaces

Reveal answer
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.

cstring position 0clist index 0ostring position 1olist index 1dstring position 2dlist index 2estring position 3elist index 3list()list()list()list()
How does each character in the original string map to a separate list element and index?

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.

Worked example

Turning a Label into Character Elements

Convert the string "sun" into a list of individual characters.

  1. 1
    Start with the string

    The original value is the single string "sun".

  2. 2
    Apply list()

    The list function separates the string into its individual characters.

  3. 3
    Inspect the elements

    The resulting list contains "s", "u", and "n" as separate elements.

Result

["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.

red,blue,greenbefore split()redlist elementbluelist elementgreenlist elementsplit at commasplit at commasplit at comma
How does a delimiter determine where the original string is cut and what segments are placed into the resulting list?
Worked example

Separating Comma-Delimited Values

Break the string "tea,coffee,water" into separate segments using the comma as the delimiter.

  1. 1
    Identify the delimiter

    The comma marks each boundary between neighboring values.

  2. 2
    Apply split()

    Calling split() with the comma separates the original string at each comma.

  3. 3
    Read the resulting list

    The list contains "tea", "coffee", and "water" as separate string elements.

Result

["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.

redlist elementcommaseparator stringred,blue,greenone stringbluelist elementgreenlist elementjoinjoinjoininserted between elements
How do multiple list elements become one string, and where is the chosen separator inserted?
Worked example

Joining Words with Spaces

Combine the list ["Lists", "are", "sequences"] into one string with a space between each element.

  1. 1
    Choose the separator

    Use a single space as the separator because the desired result should have spaces between words.

  2. 2
    Call join on the separator

    The separator performs the joining operation; the list supplies the string elements to combine.

  3. 3
    Inspect the result

    The elements become the single string "Lists are sequences".

Result

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

Individualcharactersprocessing goallist()operationWords or segmentsprocessing goalsplit()operationOne combined stringprocessing goaljoin()operationuseuseuse
Which operation should be used when separating characters, extracting segments, or recombining strings?
Processing goalOperationResulting structure
Separate every characterlist()A list with one character per element
Separate words or delimiter-defined segmentssplit()A list with one word or segment per element
Combine string elementsjoin()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 like

    Applying list() to "red blue" when the desired elements are "red" and "blue"

    Why it fails

    list() separates individual characters, not words.

    Do this instead

    Use 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 like

    Trying to use a word or segment delimiter to produce one list element for each character

    Why it fails

    split() divides at delimiters, so it is designed for words or larger segments rather than character-by-character separation.

    Do this instead
    Use list() for individual characters.
  • Common mistake

    Calling join() on the list

    What it looks like

    Treating the list as the object that supplies the separator

    Why it fails

    The join operation is called on the separator string, not on the list.

    Do this instead

    Choose 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 like

    Expecting join() to produce another list of segments

    Why it fails

    join() performs the reverse type of transformation: it combines list elements into a single string.

    Do this instead

    Use join() when the desired final structure is one string.

Practice the Choice

EASY

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.
  1. 1
    A string and a list of characters are both sequences, but they organize text differently. Use list() to create one list element per character. Use split() to create list elements from words or delimiter-defined segments. Use join() 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.

Related concepts

More concepts starting with U