Category: Python

Python: Initialize list and tuple values

April 15th, 2008 | No Comments | Print

This tutorial will show how to give a Python list an initial value.

To initialize list myTuple values to 0, with 10 items:

myTuple = [0]*10

If we need to have 5 items of a tuple with the value 0 and the rest 5 with the value 1:

myTuple = [0]*5 + [1]*5

To initialize list myList values to 0, with 10 items:

myList = (0,)*10

If we need to have 5 item of list with the value 0 and the rest 5 with the value 1:

myList = (0,)*5 + (1,)*5

For list, don’t forget the comma ‘,’. It means one item.

Recommended Reading

Like this article?

Subscribe to the  RSS feed, or to  daily email updates, right now!

Share and enjoy:  Digg  del.icio.us  TwitThis  Facebook

Leave a Reply