Search this site
Embedded Files
Python by Examples
  • Tutorial
    • Download & Install
      • Install Python on Ubuntu Linux
    • Help
  • Strings
    • comparison
    • Get safe character string
    • str() and repr()
  • argparse
    • ValueError
  • Biopython
    • Examples
      • GC content
      • Inverse seq
      • Read/write fasta
    • Install Biopython
  • Data structures
    • Dictionary
      • get keys
      • Multiple keys
    • Lists
      • 0-based indices
      • List element frequencies
      • Median
  • Debugging
  • Error
    • TabError
  • Error handling
    • Check Python version
    • Type checking (raise)
  • File operations
    • File commands
      • get module path
      • os.rename vs shutil.move
      • tar
    • Files: read & write
      • gzip compression
      • Read csv files
      • strip
  • Functions
  • Loops
    • For loops
    • if else ...
    • List Comprehension
  • Math
    • DataFrame
    • Machine Learning
      • Kernel PCA
    • Matrix Calculations
      • Plot
    • Statistics
    • types
  • Packages
    • Create own modules
    • Import Modules
      • Check modul version
    • Install packages
    • python script
  • Parallel processing
    • pool.map - multiple arguments
  • Plot
    • barplot
    • colors
      • rgb2hex
    • matplotlib
      • colormaps
      • Error
      • Transparent colors
  • Print
    • Error
  • System
    • Environment
    • External Commands
  • Time
  • WWW
    • CGI script
    • Download webpage
    • Graphics
Python by Examples

0-based indices

Lists (data structures)

Why Python uses zero-based indices and not one-based as in Matlab and R?

Why computer scientists count from zero?


(At) lunch break

Antje Jones: I had a list of letters:

>>> letter=['A','B','C','D','E']

and tried to get the second letter 'B':

>>> letter[2]

'C'

Surprisingly I got a 'C'?!   This is confusing!   A is the 0th letter?!   No one counts like that!

Mike Zero: I agree, for working with objects, 0-based makes no sense. But starting at zero is the way of thinking about time (and data-streams). We don't see the index as an label of an object (for this we use dictionaries), we see the index as a start position of a new interval: a new hour or a new block of data (reading the next 100 letters from a file). We don't 'count' from zero, we simply mean the 'first' element starts at 0.

Let's look to my morning live-stream from midnight (zero) until now:

at=['brr','brr','r','r','r','r','r','r','breakfast','goToWork','programming','meeting','lunch']

   |     |     |   |   |   |   |   |   |           |          |             |         |       |

   0     1     2   3   4   5   6   7   8 o'clock   9         10            11        12   now(end)

What I did between 8 and 10 o'clock?

>>> at[8:10]

['breakfast', 'goToWork']

..and then? What I did from 10 until midday

>>> at[10:12]

['programming', 'meeting']

Lunch was at 12 ?

>>> at[12]

'lunch'

When is the meeting?

>>> at.index('meeting')

11

ah, OK meeting starts at 11 o'clock

Antje Jones: OK, I understand, 0-based (starting with 0) is useful when working with a time-like sequences of data. But I need to work with objects: biological samples (1st, 2nd, 3rd, etc.) and 20,000 genes for which starting with 1 is more intuitive: gene[1], gene[2],...

Mike Zero: Unfortunately, it's not possible or at least very confusing to have both 0-based and 1-based indices in the same programming language. Therefore, mathematical languages working with objects (samples) are often 1-based. More general-purpose languages (C++, Java, and Python) which frequently are used to read and process data-streams, are 0-based.

In Python, you can avoid using direct indices in for loops by using  in

for element in mylist:

    print(element)

Alternatively, some commands offer a switch to 1-based indices

letter=['A','B','C','D','E']

for i, element in enumerate(letter,1):

    print(i,element)

 (1, 'A')

 (2, 'B')

And, instead of using lists, you can handle objects by using dictionaries.    

Antje Jones: Thanks a lot!


All names and characters in this dialogue are fictitious, but inspired from discussions at:

http://python-history.blogspot.it/2013/10/why-python-uses-0-based-indexing.html

http://exple.tive.org/blarg/2013/10/22/citation-needed/

http://programmers.stackexchange.com/questions/110804/why-are-zero-based-arrays-the-norm

Google Sites
Report abuse
Page details
Page updated
Google Sites
Report abuse