Python Lists

Main.PythonLists History

Hide minor edits - Show changes to markup

April 30, 2007, at 03:20 PM EST by CptnObvious999 - Added changing lists
Added lines 39-46:

Now what if you want that 'yay' to say 'boo'? Simply change it on the fly:

>>> x[1] = 'boo'
>>> print x
[[0, 1, 'z'], 'boo']
April 30, 2007, at 03:13 PM EST by CptnObvious999 -
Added line 38:
April 30, 2007, at 03:13 PM EST by CptnObvious999 - Added embed lists
Changed lines 20-37 from:

@]

to:

@]

You can even have lists inside lists!

>>> x = [ [ 0, 1, 'z' ], 'yay' ]
>>> print x[0]
[0, 1, 'z']

What happened there is it called the first object in the list which just happened to be a list so that is what it printed. So what if you want to get the 'z' out of that embeded list? Here's how you do it:

>>> print x[0][2]
z

All you do is add the number of the object you want called in the embeded list after specifying the list it is held in.

April 26, 2007, at 02:45 PM EST by jza -
Changed lines 1-20 from:
  • Recording on Lists
to:
  • Recording on Lists

Descriptions

Lists are more flexible than variables and can contain heterogeneous elements, not just alphanumeric characters.

Lists also have nestable elements, this elements can be lists, which means that you can have other multiple data types inside a list.

They are also mutable which means you can change the type.

A list is form using brackets [ ] and separate the elements using a comma.

>>> a = [ 'a', 'b', 'c', 1, 2, 3]
>>> b = [ a, 6]
>>> print b[0]
[ 'a', 'b', 'c', 1, 2, 3]
>>> print a[3]
1
April 24, 2007, at 08:27 AM EST by jza -
Added line 1:
  • Recording on Lists