Concepts / Working with Lists: Iteration and Modification

Working with Lists: Iteration and Modification

The split method converts a string into a list of words by breaking on whitespace (when called with no arguments) or on a custom delimiter (when passed as an argument).

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

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.

call methodreturnsred blue greenone stringsplit()whitespace boundary['red', 'blue','green']three list elements
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.

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

Retrieving the Second Word

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

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

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

Read the result: The expression words[1] returns 'blue'.

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.

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

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.

list()split()go nowsame string['g', 'o', ' ', 'n','o', 'w']characters and space['go', 'now']words, no space
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
python
Output
characters is ['g', 'o', ' ', 'n', 'o', 'w']
words is ['go', 'now']

Mistakes to Avoid

  • Treating index 1 as the first word

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

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

  • Expecting split to modify the original string

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

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

  • Using list when words are required

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

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

  • Forgetting to provide the delimiter

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

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

Key Takeaways

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