Skip to content
Related Articles

Related Articles

Python – Vertical Concatenation in Matrix

Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 31 Aug, 2022
Improve Article
Save Article

Given a String Matrix, perform column-wise concatenation of strings, handling variable lists lengths.

Input : [[“Gfg”, “good”], [“is”, “for”]] Output : [‘Gfgis’, ‘goodfor’] Explanation : Column wise concatenated Strings, “Gfg” concatenated with “is”, and so on. Input : [[“Gfg”, “good”, “geeks”], [“is”, “for”, “best”]] Output : [‘Gfgis’, ‘goodfor’, “geeksbest”] Explanation : Column wise concatenated Strings, “Gfg” concatenated with “is”, and so on.

Method #1 : Using loop

This is brute way in which this task can be performed. In this, we iterate for all the columns and perform concatenation. 

Python3




# Python3 code to demonstrate working of
# Vertical Concatenation in Matrix
# Using loop
 
# initializing lists
test_list = [["Gfg", "good"], ["is", "for"], ["Best"]]
 
# printing original list
print("The original list : " + str(test_list))
 
# using loop for iteration
res = []
N = 0
while N != len(test_list):
    temp = ''
    for idx in test_list:
         
        # checking for valid index / column
        try: temp = temp + idx[N]
        except IndexError: pass
    res.append(temp)
    N = N + 1
 
res = [ele for ele in res if ele]
 
# printing result
print("List after column Concatenation : " + str(res))

Output

The original list : [['Gfg', 'good'], ['is', 'for'], ['Best']]
List after column Concatenation : ['GfgisBest', 'goodfor']

Time Complexity: O(n2)

Space Complexity: O(n)

Method #2 : Using join() + list comprehension + zip_longest()

The combination of above functions can be used to solve this problem. In this, we handle the null index values using zip_longest, and join() is used to perform task of concatenation. The list comprehension drives one-liner logic.

Python3




# Python3 code to demonstrate working of
# Vertical Concatenation in Matrix
# Using join() + list comprehension + zip_longest()
from itertools import zip_longest
 
# initializing lists
test_list = [["Gfg", "good"], ["is", "for"], ["Best"]]
 
# printing original list
print("The original list : " + str(test_list))
 
# using join to concaternate, zip_longest filling values using
# "fill"
res = ["".join(ele) for ele in zip_longest(*test_list, fillvalue ="")]
 
# printing result
print("List after column Concatenation : " + str(res))

Output

The original list : [['Gfg', 'good'], ['is', 'for'], ['Best']]
List after column Concatenation : ['GfgisBest', 'goodfor']

Time Complexity: O(n2) -> (loop+join)

Space Complexity: O(n)


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!