Python: Initialize list and tuple values

Tuesday, April 15th, 2008
Advertisement

Subscribe.
Enter your email:

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

To initialize a ten items list with values set to 0:

myList = [0]*10

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

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

To initialize a ten items tuple with values set to 0:

myTuple = (0,)*10

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

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

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

If you are new here, you might want to subscribe to the RSS feed or newsletter.

Enter your email address:

Creates the exact copy of your hard disk and allows you to instantly restore the entire machine.
New Acronis True Image Home 2010 is the most reliable and easy in use backup solution. Now with online backup option!
15% Discount Code: FMAATIH2010

What else?

Like this article? Share it

 Digg  del.icio.us  TwitThis  Facebook  Reddit  StumbleUpon

2 Responses to “Python: Initialize list and tuple values”

  1. Jimmy says:

    you’ve got tuples and lists backwards. tupes are () and lists are [].

    Jimmy

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>