Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

concatenate list in python

# There are many methods to do list concatenation
# Method 01
test_list1 = [1, 4, 5, 6, 5]
test_list2 = ['p', 'q', 'r', 's']
for i in test_list2 :
    test_list1.append(i)
print(test_list1)

# Method 02
test_list1 = [1, 4, 5, 6, 5]
test_list2 = ['p', 'q', 'r', 's']
test_list3 = test_list1 + test_list2
print(test_list3)

# Method 03
test_list1 = [1, 4, 5, 6, 5]
test_list2 = ['p', 'q', 'r', 's']
res_list = [y for x in [test_list1, test_list2] for y in x]
print(res_list)

# Method 04
test_list1 = [1, 4, 5, 6, 5]
test_list2 = ['p', 'q', 'r', 's']
test_list1.extend(test_list2)
print(test_list1)

# Method 05
test_list1 = [1, 4, 5, 6, 5]
test_list2 = ['p', 'q', 'r', 's']
res_list = [*test_list1, *test_list2]
print(res_list)

# Method 6
import itertools
test_list1 = [1, 4, 5, 6, 5]
test_list2 = ['p', 'q', 'r', 's']
res_list = list(itertools.chain(test_list1, test_list2))
print(res_list)
Source by www.geeksforgeeks.org #
 
PREVIOUS NEXT
Tagged: #concatenate #list #python
ADD COMMENT
Topic
Name
6+3 =