
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Vertical Concatenation in Matrix in Python
When it is required to concatenate a matrix vertically, the list comprehension can be used.
Below is a demonstration of the same −
Example
from itertools import zip_longest my_list = [["Hi", "Rob"], ["how", "are"], ["you"]] print("The list is : ") print(my_list) my_result = ["".join(elem) for elem in zip_longest(*my_list, fillvalue ="")] print("The list after concatenating the column is : ") print(my_result)
Output
The list is : [['Hi', 'Rob'], ['how', 'are'], ['you']] The list after concatenating the column is : ['Hihowyou', 'Robare']
Explanation
The required packages are imported.
A list of list is defined, and is displayed on the console.
The list comprehension is used to zip the elements, and join them by eliminating the empty spaces.
This is assigned to a variable.
This variable is displayed as output on the console.
- Related Articles
- Check horizontal and vertical symmetry in binary matrix in C++
- Concatenation of two String Tuples in Python
- Python – Incremental Slice concatenation in String list
- How does concatenation operator work on list in Python?
- How does concatenation operator work on tuple in Python?
- Vertical Histogram in Python and Matplotlib
- What is the most efficient string concatenation method in python?
- Program to find concatenation of consecutive binary numbers in Python
- String Concatenation in Java
- Barchart with vertical labels in Python/Matplotlib
- Matrix manipulation in Python
- Initialize Matrix in Python
- Rotate Matrix in Python
- How to do string concatenation without '+' operator in Python?
- String Concatenation by + (string concatenation) operator.

Advertisements