Writing a list

A list is a container object and sequence type. They are similar to tuples except that they are homogenous and mutable. A list allows append operations. They can also be used as either a stack or queue. Unlike tuples, lists are expandable; you can add elements to a list using the append function after its creation.

Getting ready

Similar to how we saw tuples, we will see lists as fragmented codes where we will concentrate on the creation and manipulation activity instead of having a full program as we did with dictionaries.

How to do it…

Let's look at some Python scripts demonstrating the list creation and manipulation activities:

# 1.Let us look at a quick example of list creation. 
a = range(1,10)
print a
b = ["a","b","c"]
print b

# 2.List can be accessed through indexing. Indexing starts at 0.
print a[0]

# 3.With negative indexing the elements of a list are accessed from backwards.
a[-1]

# 4.Slicing is accessing a subset of list by providing two indices.
print a[1:3]  # prints [2, 3]
print a[1:]   # prints [2, 3, 4, 5, 6, 7, 8, 9]
print a[-1:]  # prints [9]
print a[:-1]  # prints [1, 2, 3, 4, 5, 6, 7, 8]

#5.List concatenation
a = [1,2]
b = [3,4]
print a + b # prints [1, 2, 3, 4]

# 6.	List  min max
print min(a),max(a)

# 7.	in and not in
if 1 in a:
    print "Element 1 is available in list a"
else:
    print "Element 1 is available in tuple a"

# 8. Appending and extending list
a = range(1,10)
print a
a.append(10)
print a

# 9.List as a stack
a_stack = []

a_stack.append(1)
a_stack.append(2)
a_stack.append(3)

print a_stack.pop()
print a_stack.pop()
print a_stack.pop()

# 10.List as queue
a_queue = []

a_queue.append(1)
a_queue.append(2)
a_queue.append(3)

print a_queue.pop(0)
print a_queue.pop(0)
print a_queue.pop(0)


# 11.	List sort and reverse
from random import shuffle
a = range(1,20)
shuffle(a)
print a
a.sort()
print a

a.reverse()
print a

How it works…

In step 1, we saw different ways of creating a list. Note that we have only homogeneous elements. There can be duplicates unlike a set. Steps 2,3,4,5,6, and 7 are similar to the tuple steps. We will not elaborate on these steps. They cover the indexing, slicing, concatenation, minmax, and in and not in operations similar to a tuple.

Step 8 presents the append and extend operations. This is where a list starts to differ from a tuple. (Of course, we know that these lists are homogeneous.) Let's look at the output of the first part of the code:

>>> a = range(1,10)
>>> print a

[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> a.append(10)
>>> print a
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>>

We can see that 10 is added to the a list.

The following output is of the second part where extend is shown:

>>> b=range(11,15)
>>> a.extend(b)
>>> print a
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
>>>

We extended the original a list by another list, b.

In step 9, we will show how a list can be used as a stack. The pop() function helps to retrieve the last element appended to the list. The output is as follows:

3
2
1

The last element to be appended is the first element to be retrieved Last In, First Out (LIFO) style as in stacks.

In step 10, we will implement a queue using a list. The pop() function with zero as a parameter indicates that the index of the element to be retrieved has been passed. The output is as follows:

1
2
3

The output adheres to the LIFO style of a queue. However, this is not a very efficient method. Popping the first element is not optimal because of the way a list is implemented. An efficient way to perform this operation is to use the deque data structure explained in the next section.

The final step details the sort and reverse operations in a list. A list has a built-in function, sort(), to sort the elements of a list. By default, it sorts in an ascending order. Sorting is explained in detail in a later section of this chapter. The reverse() function will reverse the elements of a list.

We will first create a list with elements from 1 to 19:

a = range(1,20)

We will shuffle the elements using the shuffle() function from a module random. This shuffles the elements so that we can demonstrate the sort operations. The shuffled output is as follows:

[19, 14, 11, 12, 4, 13, 17, 5, 2, 3, 1, 16, 8, 15, 18, 6, 7, 9, 10]

Now, a.sort() does an in-place sort and when we print a, we will get the following output:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

The a.reverse() is also an in-place operation that produces the following output:

[19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

There's more…

The deque stands for double-ended queue. Unlike stack and queues, which can be appended and popped in only one direction, the append and pop operations can be done at both ends with deque:

https://docs.python.org/2/library/collections.html#collections.deque

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset