Why does this Python code insert a new value to a str list but raise exception for an integer list? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why does this Python code insert a new value to a str list but raise exception for an integer list?

try: s = ['a', 'c'] n = [0, 2] s[1:1] = 'b' n[1:1] = 1 except: pass print(s[1], n[1]) # result: b 2

7th Feb 2020, 6:59 AM
Prof. Dr. Zoltán Vass
5 Answers
+ 3
slicing expects a sequence. A String ls a sequence but an Integer is not.
7th Feb 2020, 8:03 AM
Oma Falk
Oma Falk - avatar
+ 3
Jan Markus ich musste STUNDENLANG warten, damit du Erster bist
7th Feb 2020, 8:04 AM
Oma Falk
Oma Falk - avatar
+ 3
Jan Markus One python post has estimated 0.3 promille. This explains how you feel.
7th Feb 2020, 8:51 AM
Oma Falk
Oma Falk - avatar
+ 1
Jan Markus Thanks for the details! This snippet was a sololearn match challenge, that’s why it is a little bit strange. I just wanted to be sure I understand the solution.
7th Feb 2020, 8:47 AM
Prof. Dr. Zoltán Vass
0
Oma Falk Very clear and concise, thanks!
7th Feb 2020, 8:37 AM
Prof. Dr. Zoltán Vass