xxxxxxxxxx
# define two lists
list1 = [1, 2, 3]
list2 = [4, 5, 6]
# concatenate the lists
new_list = list1 + list2
print(new_list) # print as [1, 2, 3, 4, 5, 6]
# extend list1 with the elements of list2
list1.extend(list2)
print(list1) # print as [1, 2, 3, 4, 5, 6]
xxxxxxxxxx
a = [1, 2, 3]
b = [4, 5]
# method 1:
c = a + b # forms a new list with all elements
print(c) # [1, 2, 3, 4, 5]
# method 2:
a.extend(b) # adds the elements of b into list a
print(a) # [1, 2, 3, 4, 5]
xxxxxxxxxx
list1 = [1, 2, 3]
list2 = [4, 5, 6]
concatenated_list = list1 + list2
print(concatenated_list)
xxxxxxxxxx
sum([[1, 2, 3], [4, 5, 6], [7], [8, 9]],[])
# [1, 2, 3, 4, 5, 6, 7, 8, 9]
xxxxxxxxxx
list1 = [1,2]
list1 = [3,4]
concat_list1 = list1 + list2 # [1,2,3,4]
concat_list2 = list1.extend(list2) # [1,2,3,4]
concat_list3 = list1.append(list2) # [1,2,[3,4]] <-- list2 is considered 1 element
xxxxxxxxxx
list1 = ["Hello ", "take "]
list2 = ["Dear", "Sir"]
resList = [x+y for x in list1 for y in list2]
print(resList)
#['Hello Dear', 'Hello Sir', 'take Dear', 'take Sir']
xxxxxxxxxx
>>> t1 = ['a', 'b', 'c']
>>> t2 = ['d', 'e']
>>> t1.extend(t2)
>>> print(t1)
['a', 'b', 'c', 'd', 'e']
xxxxxxxxxx
""" There are 3 methods: '+', list.append, list.extend() """
# '+'
list_one = [11, 12, 13]
list_two = [14, 15, 16]
answer = list_one + list_two
--> answer: [11,12,13,14,15,16]
# 'list.append' adds items indivudually
# WARNING it treats a list as 1 item
first_list = [1, 2, 3, 4, 5]
second_list = [6, 7, 8, 9]
first_list.append(second_list)
--> first_list: [1, 2, 3, 4, 5, [6, 7, 8, 9]]
# 'extend' adds to the end, only works when both are lists
first_list = [1, 2, 3, 4, 5]
second_list = [6, 7, 8, 9]
first_list.extend(second_list)
--> first_list: [1, 2, 3, 4, 5, 6, 7, 8, 9]
xxxxxxxxxx
data1 = [1, 2, 3]
data2 = [4, 5, 6]
data = data1 + data2
print(data)
# output : [1, 2, 3, 4, 5, 6]
xxxxxxxxxx
listone = [1, 2, 3]
listtwo = [4, 5, 6]
joinedlist = listone + listtwo