0

I'm struggling to make sense of the following for a python list:

L = []
L[0:] = 'string'       # equivalently L[:] = 'string'

and here L = ['s','t','r','i', 'n', 'g']

why does python do this? I know this is the expected behaviour of running

list('string')

but I'm not sure how to explain this behaviour.

WeakLearner
  • 918
  • 14
  • 26
  • Does this answer your question? [How does assignment work with list slices?](https://stackoverflow.com/questions/10623302/how-does-assignment-work-with-list-slices) – buran Nov 07 '22 at 17:08
  • It is because you are passing only one element and since you said you want to slice the string with `L[0:]` it is doing that, if there was more than just one element that you want to add it wouldn't do that. To set the string as the first element you can simply use `L.append('string')`. – BokiX Nov 07 '22 at 17:15
  • @buran I don't think this answers my question - I'm specifically asking about the behavior when assigning a string to the whole slice L[:]. – WeakLearner Nov 07 '22 at 17:17
  • @BokiX `L='string'` convers L into a string, i'm asking about the behavior of the line `L[:] = 'string' ` – WeakLearner Nov 07 '22 at 17:17
  • Strings are kind of an odd duck in Python. They are simultaneously a single object and an iterable. – Mark Ransom Nov 07 '22 at 17:19
  • @WeakLearner I meant `L.append('string')` sorry. Read my explanation above and see if it answers you. – BokiX Nov 07 '22 at 17:19
  • @BokiX I appreciate the comment and I understand how to set the first element of L to be a specified string, but here I am trying to understand what is going on with the line `L[:] = 'string'` as opposed to asking how one should ideally do something in python – WeakLearner Nov 07 '22 at 17:20
  • @MarkRansom could you say more about this? since strings are iterable then why exactly does the right hand side of `line L[:] = 'string' ` get interpreted as `L[:] = ['s', 't','r','i','n','g']` ?? – WeakLearner Nov 07 '22 at 17:21
  • If I knew the exact mechanics behind it I'd leave an answer instead of a comment. – Mark Ransom Nov 07 '22 at 17:22
  • Check https://docs.python.org/3/reference/simple_stmts.html#assignment-statements It explains the logic/mechanics. – buran Nov 07 '22 at 17:25

1 Answers1

0

by making the left-hand-side L[:], you force the right-hand side to be an iterable (if you try, for example L[:] = 1, it will result in a TypeError). Therefore, L[:] = some_iterable_thing translates as iterate over some_iterable_thing and assign it to elements of the L list. Examples:

L[:] = 1,2,3 ==> [1,2,3]
L[:] = (1,2,3) ==> [1,2,3]
L[:] = 'string' ==> ['s','t','r','i','n','g']
L[:] = 1 ==> TypeError
L[:] = None ==> TypeError
Alberto Garcia
  • 324
  • 1
  • 11
  • So the workaround for the problem would be `L[:] = ['string']`? – Mark Ransom Nov 07 '22 at 17:25
  • why does making the left hand side `L[:]` force the right hand side to be an iterable ? – WeakLearner Nov 07 '22 at 17:25
  • 1
    @WeakLearner A slice is a sequence of elements in the target list, so you need a sequence of values to assign to each element. – Barmar Nov 07 '22 at 17:30
  • @MarkRansom to add an element to the list L, just L.append('string') – Alberto Garcia Nov 07 '22 at 17:31
  • The question isn't about appending to the list, it's about replacing the contents of the list. We should probably assume that the slice being the entire list was just a simplification for the purposes of asking the question. – Mark Ransom Nov 07 '22 at 17:36