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).
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.
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.
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'.
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.
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.
| Tool | How it divides the string | What remains in the result | Use it when |
|---|---|---|---|
| list(string) | Separates every character | Characters, including spaces and punctuation | You need individual characters |
| string.split() | Separates at whitespace | Words without the whitespace delimiters | You need words |
| string.split(delimiter) | Separates at the specified delimiter | Values without that delimiter | You need delimited fields or logical units |
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
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?
Reveal answer
Answer: east
The split produces ['north', 'south', 'east']. Since positions start at 0, index 2 identifies the third element, 'east'.
Key Takeaways
- Calling split with no argument divides a string at whitespace and returns a list of words.
- Passing a delimiter to split divides a string at that specific character, and the delimiter is removed from the resulting list.
- The returned list can be read by index, with the first element at index 0.
- split returns a new list and does not modify the original string.
- 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.