Pacific-Design.com

    
Home Index

1. Python

2. 04 List

Python / 04 List /

Python List

#!/usr/bin/python
import random

#----------------------------------------------------------------#
class MyList():

    def transform(self):

        list = ["zero", "one", "two", "three", "four", "five"]
        print "\n------ List Transformaton ------------"
    
        list.append("python");  print "Append python    =", list  
        list.remove("one");     print "Remove one       =", list  
    
        del list[0:2];          print "Delete first two =", list  
        list.sort();            print "Sort list        =", list  
        list.reverse();         print "Severse          =", list
        list.insert(2, "six");  print "Insert six       =", list
        list.pop(3);            print "Pop third        =", list


    def iterate(self):
    
        print "\n------ Iterate through the list ------"
        list = ["zero", "one", "two", "three", "four", "five"]
        for element in list:
            print element


#----------------------------------------------------------------#
if __name__ == "__main__":
    
    list = MyList()
    list.transform()
    list.iterate()

------ List Transformaton ------------ Append python = ['zero', 'one', 'two', 'three', 'four', 'five', 'python'] Remove one = ['zero', 'two', 'three', 'four', 'five', 'python'] Delete first two = ['three', 'four', 'five', 'python'] Sort list = ['five', 'four', 'python', 'three'] Severse = ['three', 'python', 'four', 'five'] Insert six = ['three', 'python', 'six', 'four', 'five'] Pop third = ['three', 'python', 'six', 'five'] ------ Iterate through the list ------ zero one two three four five

Multi Dimentional List

#!/usr/bin/python

print("----------------------------------------------")
print("           Multi Dimentional List             ")
print("----------------------------------------------")

list = [[1,2,3],[4,5,6,7],[8,9,10]]
for x in list:
    if len(x)==3:
            print x

---------------------------------------------- Multi Dimentional List ---------------------------------------------- [1, 2, 3] [8, 9, 10]