Python | Remove the last element from every row in a matrix

| |

👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!

Method # 1: Using a loop + del + slicing list
A combination of the above functions can be used to accomplish this task. In this we run a loop for each row in the matrix and remove the trailing element using del .

# Python3 code to demonstrate how it works
# Remove the last element from each row in the matrix
# Using a loop + Del + slicing list


# initialize the list

test_list = [[[ 1 , 3 , 4 ], [ 2 , 4 , 6 ], [ 3 , 8 , 1 ]]


# print original list

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


# Remove the last element from each row in the matrix
# Using a loop + Del + slicing list

for ele in test_list: del ele [ - 1 ]


# print result

print ( "Matrix after removal of rear element from rows: " + str (test_list) )

Output:

 The original list: [[1, 3, 4], [2, 4, 6], [3, 8, 1]] Matrix after removal of rear element from rows: [[1, 3], [2, 4], [ 3, 8]] 

Method # 2: Using List Comprehension + List Slicing
A combination of the above functions can also be used to accomplish this task. In this we simply iterate over each row and remove the trailing element using the list slicing.

# Python3 code to demonstrate how it works
# Remove the last element from each row in the matrix
# Using list comprehension + list slicing


# initialize the list

test_list = [[ 1 , 3 , 4 ], [ 2 , 4 , 6 ], [ 3 , 8 , 1 ]]


# print the original list

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


# Remove the last element from each row in the matrix
# Using list comprehension + list slicing

res = [ele [: - 1 ] for ele in test_list]


# print result

print ( "Matrix after removal of rear element from rows:" + str (res))

Output:

 The original list: [[1, 3, 4], [2, 4, 6], [3, 8, 1]] Matrix after removal of rear element from rows: [[1, 3], [2, 4], [3, 8]] 

👻 Read also: what is the best laptop for engineering students?

Python | Remove the last element from every row in a matrix __del__: Questions

How can I make a time delay in Python?

5 answers

I would like to know how to put a time delay in a Python script.

2973

Answer #1

import time
time.sleep(5)   # Delays for 5 seconds. You can also use a float value.

Here is another example where something is run approximately once a minute:

import time
while True:
    print("This prints once a minute.")
    time.sleep(60) # Delay for 1 minute (60 seconds).

2973

Answer #2

You can use the sleep() function in the time module. It can take a float argument for sub-second resolution.

from time import sleep
sleep(0.1) # Time in seconds

Python | Remove the last element from every row in a matrix __del__: Questions

How to delete a file or folder in Python?

5 answers

How do I delete a file or folder in Python?

2639

Answer #1


Path objects from the Python 3.4+ pathlib module also expose these instance methods:

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News


Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method