Set - 3

Question 1 :

What's a negative index? 

Answer :

Python sequences are indexed with positive numbers and negative numbers. For positive numbers 0 is the first index 1 is the second index and so forth. For negative indices -1 is the last index and -2 is the penultimate (next to last) index and so forth. Think of seq[-n] as the same as seq[len(seq)-n]. 
Using negative indices can be very convenient. For example S[:-1] is all of the string except for its last character, which is useful for removing the trailing newline from a string.

Question 2 :

How do I iterate over a sequence in reverse order?

Answer :

If it is a list, the fastest solution is 

for x in list:
"do something with x"

This has the disadvantage that while you are in the loop, the list is temporarily reversed. If you don't like this, you can make a copy. This appears expensive but is actually faster than other solutions: 

rev = list[:]
for x in rev:
<do something with x>

If it's not a list, a more general but slower solution is: 

for i in range(len(sequence)-1, -1, -1):
x = sequence[i]
<do something with x>

A more elegant solution, is to define a class which acts as a sequence and yields the elements in reverse order (solution due to Steve Majewski): 

class Rev:
def __init__(self, seq):
self.forw = seq
def __len__(self):
return len(self.forw)
def __getitem__(self, i):
return self.forw[-(i + 1)]

You can now simply write:

for x in Rev(list):
<do something with x>

Unfortunately, this solution is slowest of all, due to the method call overhead. 

With Python 2.3, you can use an extended slice syntax: 

for x in sequence[::-1]:
<do something with x>

Question 3 :

How do you remove duplicates from a list?

Answer :

If you don't mind reordering the list, sort it and then scan from the end of the list, deleting duplicates as you go: 
if List:

last = List[-1]
for i in range(len(List)-2, -1, -1):
if last==List[i]: del List[i]
else: last=List[i]

If all elements of the list may be used as dictionary keys (i.e. they are all hash able) this is often faster 

d = {}
for x in List: d[x]=x
List = d.values()


Question 4 :

How do you make an array in Python?

Answer :

Use a list:

["this", 1, "is", "an", "array"]

Lists are equivalent to C or Pascal arrays in their time complexity; the primary difference is that a Python list can contain objects of many different types. 
The array module also provides methods for creating arrays of fixed types with compact representations, but they are slower to index than lists. Also note that the Numeric extensions and others define array-like structures with various characteristics as well. 
To get Lisp-style linked lists, you can emulate cons cells using tuples: 

lisp_list = ("like", ("this", ("example", None) ) ) 

If mutability is desired, you could use lists instead of tuples. Here the analogue of lisp car is lisp_list[0] and the analogue of cdr is lisp_list[1]. Only do this if you're sure you really need to, because it's usually a lot slower than using Python lists.

Question 5 :

How do I create a multidimensional list?

Answer :

You probably tried to make a multidimensional array like this:

A = [[None] * 2] * 3 

This looks correct if you print it:

>>> A
[[None, None], [None, None], [None, None]]

But when you assign a value, it shows up in multiple places:

>>> A[0][0] = 5
>>> A
[[5, None], [5, None], [5, None]]

The reason is that replicating a list with * doesn't create copies, it only creates references to the existing objects. The *3 creates a list containing 3 references to the same list of length two. Changes to one row will show in all rows, which is almost certainly not what you want.

The suggested approach is to create a list of the desired length first and then fill in each element with a newly created list:

A = [None]*3
for i in range(3):
A[i] = [None] * 2

This generates a list containing 3 different lists of length two. You can also use a list comprehension: 

w,h = 2,3
A = [ [None]*w for i in range(h) ]

Or, you can use an extension that provides a matrix datatype; Numeric Python is the best known.