a = [1, 2, 3, 9, 10]
b = [4, 5, 6, 7, 8]
c = a[:3] + b + a[3:]
print(c)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
fruit = ['pineapple','pear']
fruitlist = ['grape', 'uuu', 'ccc']
for x in fruitlist:
fruit.insert(len(fruit),x) # len(fruit) Express basis fruit Dynamic insertion of length of
fruit
fruit = ['pineapple','pear']
fruitlist = ['grape', 'uuu', 'ccc']
for x in fruitlist:
fruit.append(x)
fruit
extend() Method add element ,extend() and append() The difference is that :extend() You don't think of lists or Yuanzu as a whole , Instead, they add the elements they contain to the list one by one .
l = ['Python', 'C++', 'Java']
# Additional elements
l.extend('C')
print(l)
# Append tuple , Yuanzu was split into multiple elements
t = ('JavaScript', 'C#', 'Go')
l.extend(t)
print(l)
# Add list , The list is also split into multiple elements
l.extend(['Ruby', 'SQL'])
print(l)
[‘Python’, ‘C++’, ‘Java’, ‘C’]
[‘Python’, ‘C++’, ‘Java’, ‘C’, ‘JavaScript’, ‘C#’, ‘Go’]
[‘Python’, ‘C++’, ‘Java’, ‘C’, ‘JavaScript’, ‘C#’, ‘Go’, ‘Ruby’, ‘SQL’]