Concepts / Working with Lists: Iteration and Modification

Working with Lists: Iteration and Modification

Use split when your goal is to work with words or other logical values. Use list when your goal is to work with individual characters.

  • Programming

From Text to Separate Values

Text from a file, user input, or another data source often arrives as one long string. If you need to count words, search for a particular word, rearrange values, or analyze individual pieces, you first need to separate the string. Python's split method creates a new list whose elements are the words or delimited values from the original string.

Key point

Use split when your goal is to work with words or other logical values. Use list when your goal is to work with individual characters.

Whitespace Splitting

Call split with no argument to divide a string wherever Python finds whitespace. The source explains whitespace as spaces, tabs, and newlines. The result is a new list, and each word becomes a separate string element. The original string is not modified.

red blue greenone stringsplit()whitespace boundary['red', 'blue','green']three list elementscall methodreturns
How does split() transform one whitespace-separated string into separate list elements?
python
Output
words is ['red', 'blue', 'green']

Reading by Index

After splitting, each word occupies one position in the resulting list. Python positions begin at 0, not 1. Therefore, index 0 identifies the first word, index 1 identifies the second word, and index 2 identifies the third word. Square brackets retrieve the element at a particular position.

0redredwords[0]1bluebluewords[1]2greengreenwords[2]returnsreturnsreturns
Which word is returned when a particular index is used on the split list?
Worked example

Retrieving the Second Word

Split the string "red blue green" and retrieve the word at index 1.

  1. 1
    Create the list

    Calling split with no argument produces the list ['red', 'blue', 'green'] because the spaces separate the words.

  2. 2
    Apply the index

    The first word is at index 0, so the second word is at index 1.

  3. 3
    Read the result

    The expression words[1] returns 'blue'.

Result

The word at index 1 is 'blue'.

python
Output
second_word is 'blue'

Custom Delimiters

Some text does not use spaces as boundaries. Values may be separated by commas, hyphens, colons, or other characters. Pass the delimiter as a string inside split's parentheses. Python divides the original string wherever that delimiter occurs, and the delimiter itself is not included in the resulting list.

red-blue-greenhyphen-separated stringsplit('-')hyphen delimiter['red', 'blue','green']delimiter removedprocessreturns
What list elements result when a string is divided at a hyphen instead of at whitespace?
python
Output
parts is ['red', 'blue', 'green']
In the real world

A comma-separated string can be divided with a comma delimiter, such as records.split(","). This pattern is useful when extracting fields from comma-separated values or other structured text formats.

List Function Versus Split

list and split both produce lists, but they divide a string in different ways. The list function creates one element for every character, including spaces and punctuation. The split method creates words or other logical units and removes the delimiter used to separate them.

go nowsame string['g', 'o', ' ', 'n','o', 'w']characters and space['go', 'now']words, no spacelist()split()
How do list() and split() differ when they receive the same string?
ToolHow it divides the stringWhat remains in the resultUse it when
list(string)Separates every characterCharacters, including spaces and punctuationYou need individual characters
string.split()Separates at whitespaceWords without the whitespace delimitersYou need words
string.split(delimiter)Separates at the specified delimiterValues without that delimiterYou need delimited fields or logical units

Choose the operation based on the units you need to work with.

python
Output
characters is ['g', 'o', ' ', 'n', 'o', 'w']
words is ['go', 'now']

Mistakes to Avoid

  • Common mistake

    Treating index 1 as the first word

    What it looks like
    words = ['red', 'blue', 'green']; words[1]
    Why it fails

    Python positions begin at 0. Index 1 identifies the second element.

    Do this instead

    Use words[0] for the first word, words[1] for the second, and so on.

  • Common mistake

    Expecting split to modify the original string

    What it looks like
    text = 'red blue'; text.split(); then expecting text to be a list
    Why it fails

    split returns a new list and leaves the original string unchanged.

    Do this instead

    Store the returned list in a variable, such as words = text.split().

  • Common mistake

    Using list when words are required

    What it looks like
    list('go now')
    Why it fails

    list separates every character and keeps the space as an element.

    Do this instead

    Use 'go now'.split() when the desired elements are words.

  • Common mistake

    Forgetting to provide the delimiter

    What it looks like
    'red-blue-green'.split()
    Why it fails

    With no argument, split uses whitespace, so the hyphens are not selected as boundaries.

    Do this instead
    Pass the desired delimiter: 'red-blue-green'.split('-').

Practice the Transformation

EASY

Given the string "north/south/east", create a list containing the three direction words and then identify the word at index 2.

Hints
  • The separator is a slash, not whitespace.
  • Pass the slash as the argument to split.
  • Remember that the first element is at index 0.

What do you think happens?

What does "north/south/east".split("/")[2] return?

  • north

  • south

  • east

  • the slash character

Reveal answer
Answer:

east

The split produces ['north', 'south', 'east']. Since positions start at 0, index 2 identifies the third element, 'east'.

Key Takeaways

  1. 1
    Calling split with no argument divides a string at whitespace and returns a list of words.
  2. 2
    Passing a delimiter to split divides a string at that specific character, and the delimiter is removed from the resulting list.
  3. 3
    The returned list can be read by index, with the first element at index 0.
  4. 4
    split returns a new list and does not modify the original string.
  5. 5
    list breaks a string into individual characters, while split is the appropriate choice for words or delimited values.

At a glance

  • Use split() to turn whitespace-separated text into a list of words.
  • Provide a delimiter such as a comma, hyphen, colon, or slash when the values are separated by a character other than whitespace.
  • Use square-bracket index notation to retrieve an element, remembering that indexing starts at 0.
  • split() returns a new list and leaves the original string unchanged.
  • Use list() for individual characters and split() for words or other delimited values.

Related concepts

More concepts starting with W