55

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

SilentGhost
  • 307,395
  • 66
  • 306
  • 293
Ambrosio
  • 3,789
  • 6
  • 22
  • 13

6 Answers6

100

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

del l[100:]
Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662
  • 6
    slicing with a[:100] is generally a better solution since it has constant performance, but del a[100:] becomes slower for larger list. – kefeizhou Jan 29 '11 at 18:28
  • 4
    kefeizhou: That might depend on what you actually want. If you really don't need the entries beyond the first 100 any more, you should delete them. Saying it is generally a better idea to keep them lying around because this takes less time than deleting them seems strange reasoning to me. – Sven Marnach Jan 29 '11 at 18:54
  • 3
    `del l[100:]` seems to not result in a copy of the elements (`id(l)` does not change, and at a low level, I can't see why a copy would be needed), which would give another reason to use this solution. Does `l[:100]` result in a copy of the elements? even in the case of `l[:] = l[:100]`? – Eric O. Lebigot Jun 14 '15 at 04:12
18

You can use list slicing:

a = a[0:100]
GWW
  • 43,129
  • 11
  • 115
  • 108
7

The items[:100] other mentioned gives you a new list which contains the first 100 items of items. If you want to modify the list in-place, either use items[:] = items[:100] (slice assignment) or while len(items) > 100: items.pop()use del items[100:] as proposed by Ned Batchelder.

2

The correct answer is, of course:

>>> x = range(10)
>>> x
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> x = x[:5]
>>> x
[0, 1, 2, 3, 4]

But, importantly, if you're interested in the values above 100 and want to pull them off one by one for whatever reason, you can also use POP()

>>> x = range(10)
>>> x
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> x.pop()
9
>>> x
[0, 1, 2, 3, 4, 5, 6, 7, 8]

You can even specify which element to pull out:

>>> x= range(10,20)
>>> x
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
>>> x.pop(3)
13
>>> x.pop()
19
>>> x
[10, 11, 12, 14, 15, 16, 17, 18]
>>> x.pop(-1)
18
[10, 11, 12, 14, 15, 16, 17]

This removes individual elements, shortening the list, without copying.

So, an obtuse and yucky answer (but also correct) would be to iterate down. I'm only going down from 12 to 8 for ease of reading here:

>>> x=range(12)
>>> for i in range(len(x), 8, -1):
...     y = x.pop()
...     print "popping x: ", y, ", x=", x
...
popping x:  11 , x= [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
popping x:  10 , x= [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
popping x:  9 , x= [0, 1, 2, 3, 4, 5, 6, 7, 8]
popping x:  8 , x= [0, 1, 2, 3, 4, 5, 6, 7]

Sure, it's not optimal, but I just ran into a situation where I needed this so I thought I'd share it here (I'm truncating a list when I see the first not-None value).

Kevin J. Rice
  • 3,337
  • 2
  • 24
  • 23
  • "This removes individual elements, shortening the list, without copying." From what I've read, `x.pop(3)` doesn't happen without copying. Instead, every element after the 3rd is shifted (copied) left one slot. – LarsH Jun 28 '23 at 18:28
2

You can do something like:

truncated = list[:100]
Bryan Ward
  • 6,443
  • 8
  • 37
  • 48
2

You can use slicing if you don't mind just simply creating a new copy of the list that contains only the elements you want... however this leaves the original list unmodified.

>>> a = [0,1,2,3,4,5,6,7,8,9]
>>> b = a[0:5]
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> b
[0, 1, 2, 3, 4]

If you really want to truncate the original list, just delete the elements you don't want by using slicing with del

>>> del a[5:]
>>> a
[0, 1, 2, 3, 4]
whaley
  • 16,075
  • 10
  • 57
  • 68