hossein asghari
hossein asghari

Reputation: 43

delete some list in 2 diensional list

I Have A 2 Dimensional List Like This ::

My_List = [[1,2,3],[4,5,6],[7,8,9],[10,11,12],[13,14,15],[16,17,18]]

I Want To Delete 3 Last of My_List And Have Outcome Like This ::

Outcome =[[1,2,3],[4,5,6],[7,8,9]]

And My Code Is This::

Outcome = My_List.pop(-3)

And This Remove The List By Index -3 And Not Delete The 3 Last List.

Can Anyone Help Me????!!!!

Upvotes: 0

Views: 37

Answers (2)

Ari
Ari

Reputation: 6178

My_List = [[1,2,3],[4,5,6],[7,8,9],[10,11,12],[13,14,15],[16,17,18]]
Outcome = My_List[:-3]
print(Outcome)

>>> [[1,2,3],[4,5,6],[7,8,9]]

My_List[:-3] is the same as My_List[0:-3], and its the same as My_List[0:3]

Upvotes: 1

VietHTran
VietHTran

Reputation: 2318

Just slice it.

Outcome=My_List[:-3]

Upvotes: 1

Related Questions