<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Lists
Some methods of list
Step1:  <code>del</code> statement can be used to remove an item from a list given its index
Step2:  <code>list()</code>
Step3:  Sort a list
<code>sorted</code><code>(list, [cmp=None[, key=None[, reverse]]])</code>
Step4:  List comprehensive
a concise way to create list
when need to create a list... think about it
Step5:  Tuples
lists and strings are two examples of sequence data types.
tuple can think like list, but it is immutable.
Note
Step6:  Sets
A set is an unordered collection with no duplicate elements
Curly brackets can use to create set, but <code>{}</code> create dictionary instead of set
<code>set()</code>
Step7:  add item to a set
Step8:  check item is in a set
Step9:  delete item
Step10:  similarly to list comprehensions, set comprehensions are also supported
Step11:  you can loop over the set
Step12:  Dictionaries
it's mapping type
Step13:  delete key in dictionary
Step14:  check if a key is in dictionary
Step15:  <code>dict.keys()</code>
Step16:  dictionary can be construct by calling
<code>dict(sequence)</code>
Step17:  <code>zip(sequence...)</code>
Step18:  Loop over a dictionary
Step19:  Generator & iterator
Generator
generator
Step20:  comprehensive syntax like list comprehensive
Step21:  Iterator
iterator
Step22:  More on looping
<code>enumerate</code><code>(sequence, start=0)</code>
Step23:  <code>dict.iteritems()</code>
<ASSISTANT_TASK:>
Python Code:
pets = ['dog', 'cat', 'pig']
print pets.index('cat')
pets.insert(0, 'rabbit')
print pets
pets.pop(1)
print pets
Explanation: Lists
Some methods of list:
<code>list.append(x)</code>: add <code>x</code> to the end
<code>list.insert(i, x)</code>: insert <code>x</code> at position <code>i</code>
<code>list.index(x)</code>: return index of the first item whose value is <code>x</code>
<code>list.pop([i])</code>: remove item at position <code>i</code>. default value is 0
End of explanation
a = range(10)
print a
del a[2]
print a
print a[:3]
del a[:3]
print a
Explanation: <code>del</code> statement can be used to remove an item from a list given its index
End of explanation
print list('i can eat glass')
Explanation: <code>list()</code>: convert a sequence to a list
End of explanation
print sorted([2, 3, 1], reverse=True)
a = [2, 3, 1]
print a.sort(reverse=True)
print a
print sorted([
    ['peter', 23],
    ['john', 30],
    ['tom', 18]
], key=lambda x: x[1])
Explanation: Sort a list
<code>sorted</code><code>(list, [cmp=None[, key=None[, reverse]]])</code>: return a new sorted list
<code>list.sort</code><code>([cmp=None[, key=None[, reverse]]])</code>: sort the current list (not creating new list)
where:
+ cmp: custom comparison function should return a negative, zero or positive number depending on whether the first argument is considered smaller than, equal to, or larger than the second argument
+ key: function extracting a comparison key from each list element.
+ reverse: if True, ascending sort otherwise, descending sort.
End of explanation
squares = []
for x in range(10):
    squares.append(x**2)
print squares
print [x**2 for x in range(10)]
array = []
for x in [1,2,3]:
    for y in [1, 2, 3]:
        if x != y:
            array.append((x, y))
print array
print [(x, y) for x in [1,2,3] for y in [1,2,3] if x != y]
Explanation: List comprehensive
a concise way to create list
when need to create a list... think about it
End of explanation
t = (1, 2, 3, 4, 5)
print t
tuple([1,2,3])
# change the tuple raise exception
t[0] = 5
Explanation: Tuples
lists and strings are two examples of sequence data types.
tuple can think like list, but it is immutable.
Note: no tuple comprehensive
End of explanation
letters = {'a', 'b', 'c', 'a'}
print letters
print set(['a', 'b', 'c', 'a'])
s = set(['a', 'b'])
s.add('c')
print s
Explanation: Sets
A set is an unordered collection with no duplicate elements
Curly brackets can use to create set, but <code>{}</code> create dictionary instead of set
<code>set()</code>: convert a sequence to a set
End of explanation
pets = { 'dog', 'cat', 'pig' }
pets.add('dog')
print pets
pets.add('fish')
print pets
Explanation: add item to a set
End of explanation
print 'fish' in pets
print 'lion' in pets
Explanation: check item is in a set
End of explanation
pets.remove('fish')
print pets
Explanation: delete item
End of explanation
letters = {x for x in 'i can eat glass'}
print letters
Explanation: similarly to list comprehensions, set comprehensions are also supported
End of explanation
for c in set('i can eat glass'):
    print c,
Explanation: you can loop over the set
End of explanation
{'a', 'b'}
tel = {'jack': 4098, 'sape': 4139}
tel['guido'] = 4127
print tel
tel['vu'] = 4910
print tel
print tel['jack']
Explanation: Dictionaries
it's mapping type: key -> value
key can be any immutable type: usually string or number
End of explanation
del tel['guido']
print tel
Explanation: delete key in dictionary
End of explanation
print 'sape' in tel
print 'foo' in tel
Explanation: check if a key is in dictionary
End of explanation
tel = {'sape': 4139, 'jack': 4098, 'guido': 4127}
print tel.keys()
print tel.values()
Explanation: <code>dict.keys()</code>: return list of keys
<code>dict.values()</code>: return list of values
End of explanation
print dict([('sape', 4139), ('jack', 4098), ('guido', 4127)])
Explanation: dictionary can be construct by calling
<code>dict(sequence)</code>: where a sequence of (key, value) pairs
End of explanation
zip([1, 2, 3], 'abc', 'ABC')
print dict(zip('abc', [1, 2, 3]))
Explanation: <code>zip(sequence...)</code>: zip sequences together
End of explanation
for name in tel:
    print name, ':', tel[name]
tel.values()
for telno in tel.values():
    print telno
Explanation: Loop over a dictionary
End of explanation
def firstn(n):
    i = 0
    while i < n:
        yield i
        i += 1
gen = firstn(10)
print range(50)
print firstn(50)
for i in range(5):
    print i,
    
print '\n--------------------'
for i in firstn(5):
    print i,
Explanation: Generator & iterator
Generator
generator: for lazy (on demand) generating of sequence of values
performance benefit!
End of explanation
for i in (x ** 2 for x in range(10)):
    print i,
Explanation: comprehensive syntax like list comprehensive
End of explanation
for i in xrange(10):
    print i,
Explanation: Iterator
iterator: is an object that enables a program to traverse though a sequence
<code>for</code> statement: loop through any iterator
End of explanation
list(enumerate(['dog', 'cat', 'pig']))
print list(enumerate(['dog', 'cat', 'pig']))
print list(enumerate(['dog', 'cat', 'pig'], start=2))
for value in enumerate(['dog', 'cat', 'pig']):
    print value
for index, value in enumerate(['dog', 'cat', 'pig']):
    print index, ':', value
Explanation: More on looping
<code>enumerate</code><code>(sequence, start=0)</code>
End of explanation
print tel
print list(tel.iteritems())
for name, telno in tel.iteritems():
    print name, ':', telno
for key in tel.iterkeys():
    print key
import os
os.listdir('.')
[file_name for file_name in os.listdir('.') if file_name.endswith('.pyc')]
filter(lambda file_name: file_name.endswith('.pyc'), os.listdir('.'))
os.remove('./main.pyc')
[os.remove(file_name) for file_name in os.listdir('.') if file_name.endswith('.pyc')]
Explanation: <code>dict.iteritems()</code>: return an iterator through items (key, value) of a dictionary
<code>dict.iterkeys()</code>: return an iterator through key of a dictionary
<code>dict.itervalues()</code>: return an iterator through value of a dictionary
End of explanation
<END_TASK>