Если вы отменяете операции индексирования, это может помочь понять, почему:
a = ['boo', [1, 2, 3]]
b = a[0:2] # this creates a new list from `a` with elements 0 and 1
b[0] # 'boo'
b[1] # [1, 2, 3]
f = b[0:1] # before this, b is the same as ['boo', [1, 2, 3]]
# retrieving only the first element [0, 1], returns a new list:
f[0] # 'boo'
f # ['foo'] (a list with just 'foo')
При создании списка с тем же содержимым, что и список перед ним ([0:2]
), получается другой результат:
c = a[0:2] # this creates a new list from `a` with elements 0 and 1
c[0] # 'boo'
c[1] # [1, 2, 3]
c[0:2] # the same as a[0:2][0:2], which is the same as just `c` as well.
a
и c
в этом случае содержат одинаковые данные.