tags:

views:

35

answers:

3

How many items can contain tuple or list in python? What will be if it is 10 000?

A: 

If you mean what the maximum size of a tuple or list is, I assume that it is very large. Most likely you would run out of memory before hitting some limit. Someone else can add to this if they have specific knowledge of the indexing, possible 2.1 or 4.2 billion items in 32-bit and 8 or 16 sextillion in 64-bit.

eruciform
A: 

You can try this yourself, interactively in the Python interpreter:

>>> tuple([0] * 10000)
(0, 0, 0, ... 0, 0)

where ... represents 9995 zeros.

Greg Hewgill
+2  A: 
import sys

print sys.maxsize
# prints some system-dependent number representing the maximum
# size most containers can hold.

Python sys module

I suspect on most platforms, sys.maxsize would return the same value as sys.maxint (which is guaranteed to be at least 2**31-1), but I doubt that's guaranteed.

Brian S
Good answer as far as it goes, but the maximum *size of a container* is not going to translate directly to a maximum *number of items* it can hold. You'd need to know a great deal about the internal representation. But even if you did, the precise value is not worth bothering to calculate, because it will certainly be enormously more than you have RAM for. E.g. on the 64-bit computer I'm typing this on, `sys.maxsize == 2**63 - 1`, but the *hardware* cannot handle more than 2**48 bytes of RAM.
Zack
Thanks it is very helpfull!
Pol
@Zack: Yes, `sys.maxsize` only tells you how many elements Python is _capable_ of indexing in the container (which is why I suspect it's generally the same value as `sys.maxint`). But you're also correct that just because Python knows how to use that many indices, it doesn't mean your computer can necessarily store that much information in the container itself.
Brian S