+ 4

Why list slicing showing differences in string list and number list?

Based upon the code below , i don't understand * if a list is there say s, then is s[1:1] work * in code, why s[1:1] which is a list of strings changes but not n[1:1] which is list of numbers ? https://code.sololearn.com/cM64cXV2LZ6l/?ref=app

3rd May 2019, 3:11 AM
Suzan
Suzan - avatar
4 Answers
+ 5
The problem with your code is that you missed []. You have to assign a list or iterable to a list slice. You should do something like this: s = ['a', 'c'] n = [0, 2] s[1:1] = ['b'] n[1:1] = [3] print(s, n)
3rd May 2019, 3:36 AM
Kartik
Kartik - avatar
+ 4
Kartik Krishnan and ~ swim ~ thanks a lot 😀 I understood where i had gone wrong.
3rd May 2019, 3:52 AM
Suzan
Suzan - avatar
+ 2
~ swim ~ Yes it should have given error but Sujata used try and except...
3rd May 2019, 3:49 AM
Kartik
Kartik - avatar
0
Help me
3rd May 2019, 3:46 PM
Joseph Conteh