my_list = ['abc', 'def']
outer_list = []
for element in my_list:
inner_list = []
for ch in element:
inner_list.append(ch)
outer_list.append(inner_list)
print(outer_list)
# output [['a', 'b', 'c'], ['d', 'e', 'f']]
My question is, why does this work? i.e., why does the, line inner_list = []
, not obliterate prior instances of inner_list? I think it has to do with the scope of outer_list being outside of the 'for' block. Perhaps once inner_list takes up residence there it is detached from the variable name? Is that close?