Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python concatenate list of lists

sum([[1, 2, 3], [4, 5, 6], [7], [8, 9]],[])
# [1, 2, 3, 4, 5, 6, 7, 8, 9]
Comment

Concatenate List

sample_list1 = [0, 1, 2, 3, 4] 
sample_list2 = [5, 6, 7, 8] 
 
result = sample_list1 + sample_list2 
 
print ("Concatenated list: " + str(result))
Comment

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)
Comment

python concatenate list of lists

x = [["a","b"], ["c"]]

result = sum(x, [])
Comment

how to concatenate all list inside list

>>> x = [["a","b"], ["c"]]
>>> [inner
...     for outer in x
...         for inner in outer]
['a', 'b', 'c']
Comment

PREVIOUS NEXT
Code Example
Python :: jsonpickle exclude py/object 
Python :: python set literal 
Python :: knowledgegraph dependencies 
Python :: is python the best robotic langauge 
Python :: how to take integer input in python 
Python :: bogo sort 
Python :: intersection of list of sets 
Python :: take space away from strings ion pyhton 
Python :: (Word or Phrase to Phone-Number Generator) python 
Python :: tkinter sin 
Python :: python filter dictionary 
Python :: bad resolution with df plot 
Python :: pd assign index from different df 
Python :: how to detect the body with cv2 
Python :: python cv2 blob detection seg fault 
Python :: COLLECTING 
Python :: python default summary statistics for all columns 
Python :: pandas turn counts into probability 
Python :: how to remove zero after decimal float python 
Python :: xpath h4 contains text 
Python :: python convert string to raw string 
Python :: how to do square roots in python 
Python :: pandas groupby and keep columns 
Python :: pandas add time to datetime 
Python :: how to make an error message in python 
Python :: find distance between two points in python 
Python :: Python If ... Else 
Python :: a list inside a list python 
Python :: python round function example 
Python :: list all pip packages 
ADD CONTENT
Topic
Content
Source link
Name
9+2 =