0

I don't quite understand what is happening in the below code. Is it creating a copy of a nested list? If so, how is it doing it? What is b equal to?

a = [[1,2,3],[4,5,6]]

b = [x[:] for x in a]
pault
  • 41,343
  • 15
  • 107
  • 149
wuoo12
  • 1
  • 1
    This is making a deep copy of the list. By default, l[:] is l[0:len(l)] – BlueSheepToken Apr 12 '19 at 21:34
  • And [this post](https://stackoverflow.com/questions/240178/list-of-lists-changes-reflected-across-sublists-unexpectedly) explains why you may want to do this. – pault Apr 12 '19 at 21:36
  • x[:] selects all items in list x, so you're selecting all indexes of list x for x in list a of x's – Akin Hwan Apr 12 '19 at 21:45

0 Answers0