how can I append various numbers at one time? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

how can I append various numbers at one time?

3rd Jul 2017, 1:08 AM
Chen
3 Answers
+ 6
@Venkatesh Pitta is right: extend() method add all items of the passed list/tupple to the targeted list, while append() method will add a list/tupple as only once new item ^^ list = [1,2,3] list.append(4) # list is now [1,2,3,4] list.append([5,6]) # list is now [1,2,3,4,[5,6]] list.extend([7,8,9]) # list is now [1,2,3,4,[5,6],7,8,9] Anyway, you can simply use list concatenation operator '+'... list = [1,2,3] list = list + [4,5,6] # list is now [1,2,3,4,5,6] # or shortener: list += [7,8,9] # list is now [1,2,3,4,5,6,7,8,9]
3rd Jul 2017, 6:24 AM
visph
visph - avatar
+ 2
look into extend method on lists. example: l = [] l.extend((1,2,3,4)) print(l) ## [1, 2, 3, 4] is the output.
3rd Jul 2017, 3:10 AM
Venkatesh Pitta
Venkatesh Pitta - avatar
- 1
The append method only takes a single argument. In order to add multiple values at once, you'll have to append a list of values. example: list_1 = [] list_1.append([1,2,3])
3rd Jul 2017, 1:23 AM
Sapphire