Python Lists

From OpenCircuits
Jump to: navigation, search

Contents

Overview

Lots of computing involves the idea of lists. For example you might want to do computations on the grades of students in a class, the health of monsters in a game, or the location of points in a drawing. In many computer languages this sort of structure is called an array. In Python there are several different structures that work like what in ordinary English are called lists. Here are some of the types of objects in Python that are "list like".

  • Lists: of course these behave as lists, that is what they are called after all. This is probably the most flexible type of "list" in python
  • Strings: "a string like this is sort of a list of numbers"
  • Tuples: lists with a bunch of restrictions on what can go in them and how they can be changed
  • Dictionaries: lists with a special look up mechanism.
  • NumPy arrays: "The NumPy array is the real workhorse of data structures for scientific and engineering applications."

Do not worry about all the variation here, start out with just strings and lists, the other types of "lists" can come later.

For a page that gives a bit more information on these see [PyMan 0.9.31 documentation 3. Strings, Lists, Arrays, and Dictionaries]

Creating a List

The basic way of creating a list is much like creating a string by typing "this is a string". For lists, either a list of strings or a list of numbers we can do the following:

# with names:
a_list_of_names      = [ "John", "Sue", "Mary", "Jane" ]
print a_list_of_names

# with names:
a_list_of_numbers    = [1, 22, 37, 93, 49, 3.1415926, -3]
print a_list_of_numbers

Getting at List Items

You can get at an item in a list by using an index into the lists, the index is just the numerical position of the item in the list with the first item being called 0 ( not in some ways the more logical 1).

a_list_of_names      = [ "John", "Sue", "Mary", "Jane" ]

print a_list_of_names[3]

String are a lot like lists so you can get at the parts of strings the same way

a_string = "a string is a lot like a list of letters"

print a_string[0]

print a_string[2]

Looping Over A List

There are a lot of instances in programming where you have a list and you want to do something to every element in the list. This is called loop because the program more or less loops back to an earlier spot in the program and then repeats almost the same action. Lets take our list of names and make a bunch ( for the purpose of printing ) of people named Smith ( run the program that will make it clearer. ( Note: that two string can be added to make a bigger string, you can also do this with lists ) This is done with the line starting with for and ending with the :, the lines in the loop ( just one here ) are indented. All the detail and more can be found at xxx

a_list_of_names      = [ "John", "Sue", "Mary", "Jane" ]
for one_name in a_list_of_names:
    print one_name + " Smith"

Sometimes you just want to loop say 20 times, a list with 0 to 20 in it might be handy for this. You can make a list with numbers in it with the range function:

print range( 20 )

Say for example we want to print the first few squares ( a number times itself )

for i in range(5):
    print i*i

Range can do a lot more see: xxx

What Can You Put In A List?

While some types of list like objects like strings and tuples are quite restrictive the more general lists can take pretty much anything ( and any type of thing ). And all the elements need not be of the same type.

a_list   = [ 1, "two", 3, 4.0, 2.5 * 2]
print a_list

You can even put lists in to list to make 2 dimensional list and so on....

b_list   = [ 1*1,   2*2,   3*3,    ]
c_list   = [ 1*1*1, 2*2*2, 3*3*3   ]
d_list   = [ "one", "two", "three", "four", "five"]
list_list = [a_list, b_list, c_list, d_list]
print list_list

More on Lists

There is lots more that can be done with lists, they can be added to, combined, sorted, reversed.... If you can think of it there is a good chance you can do it. Google say: Python list sort.

Links

Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox