简体   繁体   中英

How can i append an item more than once at the same time in Python?

I would like to append append an item more than once. eg

listA = ['AS','23','45']
listB = ['TH','67','78']

listB.append(listA.pop()*3)

print(listA)
# ['AS', '23']

print(listB)
#['TH', '67', '78', '454545']

on printing listB, it currently gives me the above list but i want it to give me # ['TH', '67', '78', '45','45','45'] instead

How can i do this.

Try using list.extend() and repeating not the string returned by pop() , but a single-element list:

>>> listA = ['AS','23','45']
>>> listB = ['TH','67','78']
>>> listB.extend([listA.pop()]*3)
>>> listB
['TH', '67', '78', '45', '45', '45']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM