The Mutable Default Trap
Completed
This function is buggy: def add_item(item, items=[]) shares one list across calls. Fix it using None as the default. Read n words and call add_item(word) for each; print each returned list.
Input
Line 1: n. Then n words.
Output
n lines - each list should contain one word.
Example
Input
3 a b c
Output
['a'] ['b'] ['c']
Editor
Python
Hints
Default values are created once, when the function is defined.
Use items=None and create a new list inside.
Solution explanation
A mutable default is shared by every call. None plus a fresh list inside is the standard fix.
Show solution
def add_item(item, items=None):
if items is None:
items = []
items.append(item)
return items
for _ in range(int(input())):
print(add_item(input()))