Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
List concatenation and repetition
๐ Scenario: You are organizing a small party and want to prepare a list of snacks to serve. You have two lists of snacks from different stores and want to combine them. Also, you want to repeat some popular snacks multiple times to make sure there is enough.
๐ฏ Goal: Build a Python program that creates two snack lists, combines them into one list, repeats some snacks, and then prints the final snack list.
๐ What You'll Learn
Create two lists of snacks with exact items
Create a variable for the number of repetitions
Concatenate the two snack lists
Repeat a snack list using the repetition variable
Print the final combined and repeated snack list
๐ก Why This Matters
๐ Real World
Combining and repeating lists is useful when managing inventories, menus, or any collection of items where you want to merge or duplicate entries easily.
๐ผ Career
Understanding list operations like concatenation and repetition is fundamental for data handling, automation scripts, and preparing data for analysis or display in many programming jobs.
Progress0 / 4 steps
1
Create two snack lists
Create a list called store1_snacks with these exact items: 'chips', 'cookies', 'nuts'. Also create a list called store2_snacks with these exact items: 'soda', 'juice'.
Python
Hint
Use square brackets [] to create lists and separate items with commas.
2
Create a repetition count
Create a variable called repeat_count and set it to the number 3. This will be used to repeat some snacks later.
Python
Hint
Just assign the number 3 to the variable repeat_count.
3
Combine and repeat snack lists
Create a new list called all_snacks by concatenating store1_snacks and store2_snacks. Then create another list called popular_snacks by repeating store1_snacks exactly repeat_count times.
Python
Hint
Use the + operator to join lists and the * operator to repeat a list.
4
Print the final snack lists
Print the all_snacks list and then print the popular_snacks list, each on its own line.
Python
Hint
Use two print() statements, one for each list.
Practice
(1/5)
1. What does the expression [1, 2] + [3, 4] do in Python?
easy
A. Creates a new list combining both lists: [1, 2, 3, 4]
B. Repeats the first list twice: [1, 2, 1, 2]
C. Creates a list of two lists: [[1, 2], [3, 4]]
D. Raises a TypeError because lists cannot be added
Solution
Step 1: Understand the + operator with lists
The + operator joins two lists into one new list containing all elements from both.
Step 2: Apply + to [1, 2] and [3, 4]
Joining these lists results in [1, 2, 3, 4].
Final Answer:
Creates a new list combining both lists: [1, 2, 3, 4] -> Option A
Quick Check:
List concatenation = combined list [OK]
Hint: Use + to join lists, not to repeat [OK]
Common Mistakes:
Thinking + repeats lists instead of joining
Confusing list concatenation with nesting
Expecting an error when adding lists
2. Which of the following is the correct syntax to repeat the list [5, 6] three times?
easy
A. [5, 6] ** 3
B. [5, 6] + 3
C. 3 + [5, 6]
D. [5, 6] * 3
Solution
Step 1: Identify the operator for repetition
The * operator repeats a list multiple times when used with an integer.
Step 2: Check syntax correctness
[5, 6] * 3 repeats the list three times: [5, 6, 5, 6, 5, 6].
C. TypeError because * expects an integer, not a list
D. ValueError because lists cannot be multiplied
Solution
Step 1: Understand list repetition rules
List repetition uses * with an integer, not another list.
Step 2: Identify the error in list_a * list_b
Multiplying two lists causes a TypeError because the right operand must be int.
Final Answer:
TypeError because * expects an integer, not a list -> Option C
Quick Check:
List * list is invalid, needs int [OK]
Hint: Right side of * must be integer for list repetition [OK]
Common Mistakes:
Trying to multiply two lists directly
Expecting concatenation with * operator
Confusing syntax error with type error
5. You have two lists: fruits = ['apple', 'banana'] veggies = ['carrot'] How do you create a new list that repeats the fruits twice and veggies three times, then combines them all in order?