Please tell me how to combine multiple lists in Python

Asked 2 years ago, Updated 2 years ago, 53 views

list1 = [1,2,3]
list2 = [4,5,6]
list3 = [7,8,9]

When you say there's something like this,

result = [1,2,3,4,5,6,7,8,9] Please tell me the function that makes it

list1.append(list2) If you do [1, 2, 3, [4, 5, 6]] It comes out like this ㅜ<

list python

2022-09-21 21:45

1 Answers

Python doesn't need to use a function to concatenate a list I'll tell you two ways not to use a function and how to use a function.

result = list1 + list2 + list3
result = []
result.extend(list1)
result.extend(list2)
result.extend(list3)

The difference is list1+list2 returns the list list1.extend(list2) attaches an element of list2 to list1, so nothing returns. So list1.extend(list2).You can't write like extend(list3).


2022-09-21 21:45

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.