1# Basic syntax:
2first_list.append(second_list) # Append adds the the second_list as an
3# element to the first_list
4first_list.extend(second_list) # Extend combines the elements of the
5# first_list and the second_list
6
7# Note, both append and extend modify the first_list in place
8
9# Example usage for append:
10first_list = [1, 2, 3, 4, 5]
11second_list = [6, 7, 8, 9]
12first_list.append(second_list)
13print(first_list)
14--> [1, 2, 3, 4, 5, [6, 7, 8, 9]]
15
16# Example usage for extend:
17first_list = [1, 2, 3, 4, 5]
18second_list = [6, 7, 8, 9]
19first_list.extend(second_list)
20print(first_list)
21--> [1, 2, 3, 4, 5, 6, 7, 8, 9]
1a = [1, 2, 3]
2b = [4, 5]
3
4# method 1:
5c = a + b # forms a new list with all elements
6print(c) # [1, 2, 3, 4, 5]
7
8# method 2:
9a.extend(b) # adds the elements of b into list a
10print(a) # [1, 2, 3, 4, 5]
1x = [["a","b"], ["c"]]
2
3result = sum(x, [])
4# This combines the lists within the list into a single list
1first_list = ["1", "2"]
2second_list = ["3", "4"]
3
4# Multiple ways to do this:
5first_list += second_list
6first_list = first_list + second_list
7first_list.extend(second_list)
8
1#define lists
2my_list = ["a", "b"]
3other_list = [1, 2]
4
5#extend my_list by adding all the values from other_list into my list
6my_list.extend(other_list)
7
8# output: ['a', 'b', 1, 2]