Back to questions
JuniorTheoryCommon
What does the program print?
a = [1, 2, 3]
b = a
c = a[:]
b.append(4)
print(len(a), len(c))4 3
- Expecting
3 3, as ifb = acopied the list: it gives the same list a second name, sob.append(4)adds toaas well. - Expecting
4 4, as ifa[:]were another name fora: a full slice builds a new list, sockeeps its 3 elements.
- What does
print(a is b, a is c)print? - Name two other ways to copy a list besides
a[:].