I am doing a hackerrack problem and I solved it but using something which I think is very unpythonic.
So, the idea is to parse a line which gives you a command and an operation on a list and to execute it on a list. I ended up with this ugly if/else logic as:
l = []
num_commands = int(raw_input())
for i in range(num_commands):
current = raw_input().split()
if current[0] == 'insert':
params = map(int, current[1:])
l.insert(params[0], params[1])
elif current[0] == 'print':
print l
elif current[0] == 'sort':
l.sort()
elif current[0] == 'pop':
l.pop()
elif current[0] == 'reverse':
l.reverse()
elif current[0] == 'remove':
l.remove(int(current[1]))
elif current[0] == 'append':
l.append(int(current[1]))
This is ugly but it works. I was wondering if there is a more elegant way to do the same perhaps using some dictionary lookup which binds a string to some member function for a particular object instance?