Unit 11.2A · Term 2

Creating Lists & split/join

A list is Python's most versatile data structure — an ordered, mutable collection that can hold any type of data. The split() and join() methods provide powerful ways to convert between strings and lists.

Learning Objectives

  • 11.2.3.1 Create lists in Python
  • 11.2.3.2 Use the split and join methods

Lesson Presentation

11.2A-lesson-13-lists-create.pdf · Slides for classroom use

Conceptual Anchor

The Shopping List Analogy

A list is like a shopping list: items are in a specific order (first item, second item...), you can add new items, remove items, or change an item (buy apples instead of oranges). split() is like tearing a long receipt into individual items. join() is like gluing them back together with a separator.

Rules & Theory

Creating Lists

# Method 1: Direct assignment fruits = ["apple", "banana", "cherry"] numbers = [1, 2, 3, 4, 5] mixed = [1, "hello", 3.14, True] # Method 2: Empty list empty = [] also_empty = list() # Method 3: list() from other types chars = list("Python") # ['P', 'y', 't', 'h', 'o', 'n'] from_range = list(range(5)) # [0, 1, 2, 3, 4] from_set = list({3, 1, 2}) # [1, 2, 3] (order may vary) # Method 4: List repetition zeros = [0] * 5 # [0, 0, 0, 0, 0] pattern = [1, 2] * 3 # [1, 2, 1, 2, 1, 2] # Method 5: List comprehension squares = [x**2 for x in range(1, 6)] # [1, 4, 9, 16, 25] evens = [x for x in range(20) if x % 2 == 0] # [0, 2, 4, ..., 18] # Method 6: Building from input n = int(input("How many? ")) my_list = [] for i in range(n): val = int(input(f"Value {i+1}: ")) my_list.append(val)

split() — String → List

# Split by whitespace (default) text = "Hello World Python" words = text.split() print(words) # ['Hello', 'World', 'Python'] # Split by specific delimiter date = "2025-02-19" parts = date.split("-") print(parts) # ['2025', '02', '19'] csv = "Ali,95,A" data = csv.split(",") print(data) # ['Ali', '95', 'A'] # Reading multiple numbers on one line nums = input("Enter numbers: ").split() # e.g., "10 20 30" # nums = ['10', '20', '30'] — still strings! nums = list(map(int, nums)) # [10, 20, 30] — now integers

join() — List → String

# Syntax: separator.join(list_of_strings) words = ['Hello', 'World', 'Python'] print(" ".join(words)) # "Hello World Python" print("-".join(words)) # "Hello-World-Python" print(", ".join(words)) # "Hello, World, Python" print("".join(words)) # "HelloWorldPython" # join() only works with strings! nums = [1, 2, 3] # print("-".join(nums)) # TypeError! print("-".join(str(n) for n in nums)) # "1-2-3" ✓

split() and join() are Inverses

"-".join("2025-02-19".split("-")) = "2025-02-19". They undo each other when using the same separator.

Worked Examples

1 Processing CSV Data

# Simulate reading a line from a CSV file line = "Aisha,16,95.5,A" data = line.split(",") name = data[0] age = int(data[1]) score = float(data[2]) grade = data[3] print(f"Student: {name}, Age: {age}, Score: {score}, Grade: {grade}")

2 Reading Multiple Numbers from One Line

# User enters: 10 20 30 40 50 line = input("Enter numbers separated by spaces: ") nums = list(map(int, line.split())) print("Numbers:", nums) print("Sum:", sum(nums)) print("Average:", sum(nums) / len(nums))

3 Reversing Words in a Sentence

sentence = "Python is awesome" words = sentence.split() # ['Python', 'is', 'awesome'] words.reverse() # ['awesome', 'is', 'Python'] result = " ".join(words) # "awesome is Python" print(result) # One-liner version: print(" ".join(sentence.split()[::-1]))

Pitfalls & Common Errors

split() Returns Strings

"10 20 30".split() gives ['10', '20', '30'] — strings, not integers! You must convert: list(map(int, ...)).

join() Requires All Strings

", ".join([1, 2, 3]) raises TypeError. Convert to strings first: ", ".join(str(x) for x in [1, 2, 3]).

Mutable Default Trap

a = [0] * 3 creates [0, 0, 0] — three separate integers (safe). But a = [[]] * 3 creates three references to the SAME inner list (dangerous!).

Pro-Tips for Exams

List Tips

  • split() without arguments splits by any whitespace and removes empty strings
  • split(",", 1) limits to 1 split: "a,b,c".split(",", 1)['a', 'b,c']
  • List comprehension is both faster and more Pythonic than a loop + append
  • To read N integers on one line: list(map(int, input().split()))

Graded Tasks

Remember

List 5 different ways to create a list in Python.

Understand

What is the difference between "hello world".split() and "hello world".split(" ")?

Apply

Write a program that reads a sentence and creates a list of word lengths (e.g., "Hello World" → [5, 5]).

Apply

Read 5 numbers on one line separated by commas. Split them into a list, convert to integers, and print their sum.

Analyze

What does list(range(10, 0, -2)) produce? What does [x**2 for x in range(5) if x % 2 != 0] produce?

Create

Build a "sentence scrambler" that takes a sentence, shuffles the word order randomly, and prints the scrambled version using split/join.

Self-Check Quiz

1. What does "a,b,c".split(",") return?
Click to reveal answer
2. What does '-'.join(['x', 'y', 'z']) return?
Click to reveal answer
3. What does [0] * 4 create?
Click to reveal answer
4. How do you read 3 integers from one line?
Click to reveal answer
5. What does [x*2 for x in range(4)] produce?
Click to reveal answer