Lists

The list type is an ordered sequence type similar to the tuple type. All the sequence functions and the slicing that we have seen working with strings and tuples work in exactly the same way for lists. What distinguishes tuples from lists is that lists are mutable and have methods that we can use to modify them.

Python

And whereas tuples are created using parentheses, lists are created using square brackets (or by using the list() constructor).

Let us look at some slicing examples that extract parts of a list:

>>> fruit = ["Apple", "Hawthorn", "Loquat", "Medlar", "Pear", "Quince"]

'Quince'

Here, we have used the familiar slicing syntax that we have already used for strings and tuples.

Because lists are mutable we can insert and delete list items. This is achieved by using method calls, or by using the slicing syntax where slices are used on both sides of the assignment operator. First we will look at the method calls:

>>> fruit.insert(4, "Rowan") >>> fruit

['Apple', 'Hawthorn', 'Loquat', 'Medlar', 'Rowan', 'Pear',

['Apple', 'Hawthorn', 'Loquat', 'Medlar', 'Pear', 'Quince']

We have inserted a new item and then deleted it, using a method call and an operator. The del statement is used to remove an item at a particular index position, whereas the remove() method is used to remove an item that matches remove()'s parameter. So, in this example, we could also have less efficiently deleted using fruit.remove("Rowan").

Now we will do the same thing using slicing:

>>> fruit[4:4] = ["Rowan"] >>> fruit

['Apple', 'Hawthorn', 'Loquat', 'Medlar', 'Rowan', 'Pear', 'Quince']

['Apple', 'Hawthorn', 'Loquat', 'Medlar', 'Pear', 'Quince']

When we assigned "Rowan" we used square brackets because we were inserting a list slice (a one-item list) into a list slice. If we had omitted the brackets, Python would have treated the word "Rowan" as a list in its own right, and would have inserted "R", "o", and so on, as separate items.

Table 1.4 Selected List Methods and Functions

Syntax Description x in L Returns True if item x is in list L

x not in L Returns True if item x is not in list L

L + m Returns a list containing all the items of list L and of list m; the extend() method does the same but more efficiently len(L) Returns the length of list L

L.count(x) Returns the number of times item x occurs in list L

L.index(x) Returns the index position of the leftmost occurrence of item x in list L, or raises a ValueError exception

L.append(x) Appends item x to the end of list L

L.extend(m) Appends all list m's items to the end of list L

L.insert(i, x) Insertsitem x into list L at index position int i

L.remove(x) Removes the leftmost occurrence of item x from list L, or raises a ValueError exception if no x is found

L.pop() Returns and removes the rightmost item of list L

L.pop(i) Returns and removes the item at index position int i in L

L.reverse() Reverses list L in-place

L.sort() Sorts list L in-place; this method accepts optional argu ments such as a comparison function or a "key" to facilitate DSU (decorate, sort, undecorate) sorting

When inserting using slices, the source and target slices can be of different lengths. If the target slice is of zero length, such as fruit[4:4], only insertion takes place; but if the target's length is greater than zero, the number of items in the target slice are replaced by the items in the slice that is inserted. In this example, we replaced a one-item slice with a zero-item slice, effectively deleting the one item.

Here are a few more examples:

>>> fruit[2:3] = ["Plum", "Peach"] >>> fruit

['Apple', 'Hawthorn', 'Plum', 'Peach', 'Medlar', 'Quince'] >>> fruit[4:4] = ["Apricot", "Cherry", "Greengage"] >>> fruit

['Apple', 'Hawthorn', 'Plum', 'Peach', 'Apricot', 'Cherry', 'Greengage', 'Medlar', 'Quince'] >>> bag = fruit[:] >>> bag

['Apple', 'Hawthorn', 'Plum', 'Peach', 'Apricot', 'Cherry', 'Greengage', 'Medlar', 'Quince']

Was this article helpful?

0 0
Tuberminator

Tuberminator

The main focus of this report is to show how to get involved in video marketing on the run, how to rank quickly on YouTube and Google using FREE semi-automatic tools and services. QUICKLY AND FREE. I will show methods and techniques I use to rank my videos, as well as free resources and tools to make video clips, to get backlinks and free traffic.

Get My Free Ebook


Post a comment