list append()和extend()区别

2019-04-24  本文已影响0人  脏脏包盛

append()

my_list = ['geeks', 'for'] 
my_list.append('geeks') 
print(my_list) 

output:

['geeks', 'for', 'geeks']
my_list = ['geeks', 'for', 'geeks'] 
another_list = [6, 0, 4, 1] 
my_list.append(another_list) 
print(my_list) 

output:

['geeks', 'for', 'geeks', [6, 0, 4, 1]]

extend()

my_list = ['geeks', 'for'] 
another_list = [6, 0, 4, 1] 
my_list.extend(another_list) 
print(my_list)

outputs:

['geeks', 'for', 6, 0, 4, 1]
my_list = ['geeks', 'for'] 
another_list = [6, 0, 4, [1, 2]] 
my_list.extend(another_list) 
print(my_list)

output:

['geeks', 'for', 6, 0, 4, [1, 2]]
my_list = ['geeks', 'for', 6, 0, 4, 1] 
my_list.extend('geeks') 
print(my_list)

outputs:

['geeks', 'for', 6, 0, 4, 1, 'g', 'e', 'e', 'k', 's']
上一篇下一篇

猜你喜欢

热点阅读