+ 2

Python - Why is 'b' inserted in array and 1 isn't?

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

15th Mar 2020, 5:17 PM
Paolo De Nictolis
Paolo De Nictolis - avatar
3 Antworten
+ 5
b is a string and so an iterable but 1 is an integer and no iterable. Sushi operator needs iterable.
15th Mar 2020, 5:58 PM
Oma Falk
Oma Falk - avatar
+ 2
I understood)) you can change the code like this: n[1:1] = [1] use this code to check the object: from collections.abc import Iterable var_1 = "b" var_2 = 1 if isinstance(var_1, Iterable): print(f"var_1 (value: {var_1}) is iterable") else: print(f"var_1 (value: {var_1}) is no iterable") if isinstance(var_2, Iterable): print(f"var_2 (value: {var_2}) is iterable") else: print(f"var_2 (value: {var_2}) is no iterable")
16th Mar 2020, 8:36 AM
Valerii Mamontov
Valerii Mamontov - avatar
0
Interest question! type(s) and type(n) returned list... I don't know answer.
15th Mar 2020, 7:15 PM
Valerii Mamontov
Valerii Mamontov - avatar