Monday, July 25, 2011

Python Lists : Code Examples for learning

print "This function creates a list."
def makelist():
    a = []
    for i in range(1, 20):
        a.append(i)
        print ("appending", i, ":", a)
    return a
makelist()

How does this work?

hints

First you create and empty list and fill it later []
you add item one by one - it get added to last

Why lists are powerful than tuples
What is the advantage of dictioneries?
Can you have a tuple and a dictionary in a list?
Can you also add an  object to it?


#building a list from another list
L1 = ['spa', 'spa', 'spa', 'beans', None, 'spa', 'spa']
L2 = []
for i in L1:
    if i is not None:
        L2.append(i)
print (L2)
#
#




No comments:

Post a Comment