Python | Vertical row of the Nth column in the matrix

Python Methods and Functions

Method: Using List Comprehension + join()
We achieve the goal in this method in 2 steps. In the 1st step, the elements of the Nth column are retrieved using a list comprehension. In a second step, these elements are combined to perform character to string conversion.

# Python3 code to demonstrate how it works
# Nth column vertical line in matrix
# Using join () + list comprehension

 
# initializing list

test_list = [( 'a' , 'g' , ' v' ), ( 'e' , ' f ' , 8 ), ( < code class = "string"> 'b' , ' g' , 0 )]

 
# print list

print ( "The original list:" + str (test_list))

 
# initialization N- th column

N = 1

 
# Nth column is a vertical line in the matrix
# Using join () + list comprehension

temp = [sub [N] for sub in test_list]

res = "". join (temp)

 
# Print result

print ( "Constructed vertical string: " + str (res))

Output:

 The original list: [('a ',' g', 'v'), (' e', 'f', 8), (' b', 'g', 0)] Constructed vertical string: gfg 




Get Solution for free from DataCamp guru