+ 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])
4 Réponses
+ 5
b is a string and so an iterable but 1 is an integer and no iterable.
Sushi operator needs iterable.
+ 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")
0
Interest question!
type(s) and type(n) returned list...
I don't know answer.