Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I truncate a list?

Tags:

python

list

If I have a list and want to truncate it so it is no more than 100 items, how do I do this?

like image 246
Ambrosio Avatar asked Jan 29 '11 18:01

Ambrosio


People also ask

How do you shorten the length of a list in Python?

Introduction the Python reduce() function The reduce() function applies the fn function of two arguments cumulatively to the items of the list, from left to right, to reduce the list into a single value.

How do I remove part of a list?

There are three ways in which you can Remove elements from List: Using the remove() method. Using the list object's pop() method. Using the del operator.


1 Answers

To modify the list in place (rather than make a shorter copy of the list), use:

del l[100:] 
like image 114
Ned Batchelder Avatar answered Oct 03 '22 04:10

Ned Batchelder