Jun 22, 2018

Learning lists - all operations

#!/usr/local/bin/python2.7

'''
    File name: list_all_operations.py
    Author: Prabhath Kota
    Date: June 22, 2018
    Python Version: 2.7
'''

freedom_fighters = ["Lala Lajipati Rai", "Dadabhai Naoroji", "Rajendra Prasad", "Sarojini Naidu", "Dadabhai Naoroji", "Lal Bahadur Shastri"]
print freedom_fighters
#['Lala Lajipati Rai', 'Dadabhai Naoroji', 'Rajendra Prasad', 'Sarojini Naidu', 'Dadabhai Naoroji', 'Lal Bahadur Shastri']

print '---------------index---------------'
print freedom_fighters[0]
print freedom_fighters[-1]
print freedom_fighters[2:]
print freedom_fighters[:-1]
print freedom_fighters[2:4]

#Lala Lajipati Rai
#Lal Bahadur Shastri
#['Rajendra Prasad', 'Sarojini Naidu', 'Dadabhai Naoroji', 'Lal Bahadur Shastri']
#['Lala Lajipati Rai', 'Dadabhai Naoroji', 'Rajendra Prasad', 'Sarojini Naidu', 'Dadabhai Naoroji']
#['Rajendra Prasad', 'Sarojini Naidu']

print '---------------insert in index---------------'
freedom_fighters[2] = 'Subhash Chandra Bose'
print freedom_fighters
#['Lala Lajipati Rai', 'Dadabhai Naoroji', 'Subhash Chandra Bose', 'Sarojini Naidu', 'Dadabhai Naoroji', 'Lal Bahadur Shastri']

print '---------------Add Element append vs extend---------------'
print freedom_fighters
#Append - appends as a single item
freedom_fighters.append('Subhash Chandra Bose')
print freedom_fighters
freedom_fighters.pop()
freedom_fighters.append(['Subhash Chandra Bose', 'Lal Bahadur Shastri'])
print freedom_fighters
freedom_fighters.pop()
#Extend - it adds multiple values
freedom_fighters.extend(['Subhash Chandra Bose', 'Lal Bahadur Shastri'])
print freedom_fighters
freedom_fighters.pop()
freedom_fighters.pop()
print freedom_fighters

#['Lala Lajipati Rai', 'Dadabhai Naoroji', 'Subhash Chandra Bose', 'Sarojini Naidu', 'Dadabhai Naoroji', 'Lal Bahadur Shastri']
#['Lala Lajipati Rai', 'Dadabhai Naoroji', 'Subhash Chandra Bose', 'Sarojini Naidu', 'Dadabhai Naoroji', 'Lal Bahadur Shastri', 'Subhash Chandra Bose', ['Subhash Chandra Bose', 'Lal Bahadur Shastri']]
#['Lala Lajipati Rai', 'Dadabhai Naoroji', 'Subhash Chandra Bose', 'Sarojini Naidu', 'Dadabhai Naoroji', 'Lal Bahadur Shastri', 'Subhash Chandra Bose', 'Subhash Chandra Bose', 'Lal Bahadur Shastri']
#['Lala Lajipati Rai', 'Dadabhai Naoroji', 'Subhash Chandra Bose', 'Sarojini Naidu', 'Dadabhai Naoroji', 'Lal Bahadur Shastri']

print '---------------Remove Element by value---------------'
print freedom_fighters
freedom_fighters.remove('Subhash Chandra Bose')
print freedom_fighters

#['Lala Lajipati Rai', 'Dadabhai Naoroji', 'Subhash Chandra Bose', 'Sarojini Naidu', 'Dadabhai Naoroji', 'Lal Bahadur Shastri']
#['Lala Lajipati Rai', 'Dadabhai Naoroji', 'Sarojini Naidu', 'Dadabhai Naoroji', 'Lal Bahadur Shastri']

print '-----------delete by index---------------------'
print freedom_fighters
del freedom_fighters[-1] #removes index element
del freedom_fighters[1] #removes index element
print freedom_fighters

#['Lala Lajipati Rai', 'Dadabhai Naoroji', 'Sarojini Naidu', 'Dadabhai Naoroji', 'Lal Bahadur Shastri']
#['Lala Lajipati Rai', 'Sarojini Naidu', 'Dadabhai Naoroji']

print '---------------By Ref---------------'
print freedom_fighters

#copy by Reference
freedom_fighters_1 = freedom_fighters
freedom_fighters_1.append('Bhagat Singh')
print freedom_fighters
print freedom_fighters_1

#['Lala Lajipati Rai', 'Sarojini Naidu', 'Dadabhai Naoroji']
#['Lala Lajipati Rai', 'Sarojini Naidu', 'Dadabhai Naoroji', 'Bhagat Singh']
#['Lala Lajipati Rai', 'Sarojini Naidu', 'Dadabhai Naoroji', 'Bhagat Singh']

print '---------------By Value -----------'
#copy by value
freedom_fighters_2 = freedom_fighters[:]
freedom_fighters_2.append('Sardar Vallabhbhai Patel')
print freedom_fighters
print freedom_fighters_2

#['Lala Lajipati Rai', 'Sarojini Naidu', 'Dadabhai Naoroji', 'Bhagat Singh']
#['Lala Lajipati Rai', 'Sarojini Naidu', 'Dadabhai Naoroji', 'Bhagat Singh', 'Sardar Vallabhbhai Patel']

print '---------------List Comprehension ---------------'

#This gives list
print [i + ' - India Salutes you' for i in freedom_fighters]

#tuple - gives generator object
generator_obj = (i + ' - India Salutes you' for i in freedom_fighters)
print generator_obj
for each in generator_obj:
print each

#['Lala Lajipati Rai - India Salutes you', 'Sarojini Naidu - India Salutes you', 'Dadabhai Naoroji - India Salutes you', 'Bhagat Singh - India Salutes you']
#<generator object <genexpr> at 0x0000000001EB41B0>
#Lala Lajipati Rai - India Salutes you
#Sarojini Naidu - India Salutes you
#Dadabhai Naoroji - India Salutes you
#Bhagat Singh - India Salutes you

print '---------------Sorted()---------------'
print freedom_fighters
print sorted(freedom_fighters)
print freedom_fighters

#['Lala Lajipati Rai', 'Sarojini Naidu', 'Dadabhai Naoroji', 'Bhagat Singh']
#['Bhagat Singh', 'Dadabhai Naoroji', 'Lala Lajipati Rai', 'Sarojini Naidu']
#['Lala Lajipati Rai', 'Sarojini Naidu', 'Dadabhai Naoroji', 'Bhagat Singh']

print '---------------list.reverse() ---------------'
print freedom_fighters
print freedom_fighters.reverse() #Returns None but reverse itself
print freedom_fighters

#['Lala Lajipati Rai', 'Sarojini Naidu', 'Dadabhai Naoroji', 'Bhagat Singh']
#None
#['Bhagat Singh', 'Dadabhai Naoroji', 'Sarojini Naidu', 'Lala Lajipati Rai']

print '---------------list.sort() ---------------'
print freedom_fighters
print freedom_fighters.sort() #Returns None but sorts itself
print freedom_fighters

#['Bhagat Singh', 'Dadabhai Naoroji', 'Sarojini Naidu', 'Lala Lajipati Rai']
#None
#['Bhagat Singh', 'Dadabhai Naoroji', 'Lala Lajipati Rai', 'Sarojini Naidu']

print '---------------multiply---------------'
freedom_fighters2 = freedom_fighters * 2
print freedom_fighters2
#['Bhagat Singh', 'Dadabhai Naoroji', 'Lala Lajipati Rai', 'Sarojini Naidu', 'Bhagat Singh', 'Dadabhai Naoroji', 'Lala Lajipati Rai', 'Sarojini Naidu']

print '---------------check if item exits---------------'
print freedom_fighters
print 'Bhagat Singh' in freedom_fighters
print 'Sardar Vallabhbhai Patel' in freedom_fighters

#['Bhagat Singh', 'Dadabhai Naoroji', 'Lala Lajipati Rai', 'Sarojini Naidu']
#True
#False

print '---------------Remove Duplicates---------------'
print freedom_fighters
freedom_fighters.append('Mahatma Gandhi')
freedom_fighters.append('Mahatma Gandhi')
print freedom_fighters
freedom_fighters_no_dup = []
[freedom_fighters_no_dup.append(x) for x in freedom_fighters if x not in freedom_fighters_no_dup]
print freedom_fighters_no_dup
print list(set(freedom_fighters)) #set - unordered collection data type, this kills the original order

#['Bhagat Singh', 'Dadabhai Naoroji', 'Lala Lajipati Rai', 'Sarojini Naidu']
#['Bhagat Singh', 'Dadabhai Naoroji', 'Lala Lajipati Rai', 'Sarojini Naidu', 'Mahatma Gandhi', 'Mahatma Gandhi']
#['Bhagat Singh', 'Dadabhai Naoroji', 'Lala Lajipati Rai', 'Sarojini Naidu', 'Mahatma Gandhi']
#['Mahatma Gandhi', 'Bhagat Singh', 'Lala Lajipati Rai', 'Dadabhai Naoroji', 'Sarojini Naidu']

print '---------------Convert to tuple---------------'
print freedom_fighters
print tuple(freedom_fighters)
print list(tuple(freedom_fighters))

#['Bhagat Singh', 'Dadabhai Naoroji', 'Lala Lajipati Rai', 'Sarojini Naidu', 'Mahatma Gandhi', 'Mahatma Gandhi']
#('Bhagat Singh', 'Dadabhai Naoroji', 'Lala Lajipati Rai', 'Sarojini Naidu', 'Mahatma Gandhi', 'Mahatma Gandhi')
#['Bhagat Singh', 'Dadabhai Naoroji', 'Lala Lajipati Rai', 'Sarojini Naidu', 'Mahatma Gandhi', 'Mahatma Gandhi']

print '---------------length of array---------------'
print freedom_fighters
print len(freedom_fighters) 

#['Bhagat Singh', 'Dadabhai Naoroji', 'Lala Lajipati Rai', 'Sarojini Naidu', 'Mahatma Gandhi', 'Mahatma Gandhi']
#6

print '---------------Find count of Element---------------'
freedom_fighters.append('Bhagat Singh')
print freedom_fighters
print freedom_fighters.count('Bhagat Singh')

#['Bhagat Singh', 'Dadabhai Naoroji', 'Lala Lajipati Rai', 'Sarojini Naidu', 'Mahatma Gandhi', 'Mahatma Gandhi', 'Bhagat Singh']
#2

print '---------------Pop---------------'
#pop() method removes and returns the last item if index is not provided
print freedom_fighters
freedom_fighters.pop()
print freedom_fighters

#['Bhagat Singh', 'Dadabhai Naoroji', 'Lala Lajipati Rai', 'Sarojini Naidu', 'Mahatma Gandhi', 'Mahatma Gandhi', 'Bhagat Singh']
#['Bhagat Singh', 'Dadabhai Naoroji', 'Lala Lajipati Rai', 'Sarojini Naidu', 'Mahatma Gandhi', 'Mahatma Gandhi']

print '---------------Insert---------------'
#When you insert(-3, 0) you are inserting 0 before index -3. 
#When it says "before" it has nothing to do with the direction of travel.
print freedom_fighters
freedom_fighters.insert(0,'Sarojini Naidu')
freedom_fighters.insert(len(freedom_fighters),'Sarojini Naidu')
print freedom_fighters
freedom_fighters.insert(-1,'Bhagat Singh')
print freedom_fighters
del freedom_fighters[0]
freedom_fighters.pop()
freedom_fighters.pop()
print freedom_fighters

#['Bhagat Singh', 'Dadabhai Naoroji', 'Lala Lajipati Rai', 'Sarojini Naidu', 'Mahatma Gandhi', 'Mahatma Gandhi']
#['Sarojini Naidu', 'Bhagat Singh', 'Dadabhai Naoroji', 'Lala Lajipati Rai', 'Sarojini Naidu', 'Mahatma Gandhi', 'Mahatma Gandhi', 'Sarojini Naidu']
#['Sarojini Naidu', 'Bhagat Singh', 'Dadabhai Naoroji', 'Lala Lajipati Rai', 'Sarojini Naidu', 'Mahatma Gandhi', 'Mahatma Gandhi', 'Bhagat Singh', 'Sarojini Naidu']
#['Bhagat Singh', 'Dadabhai Naoroji', 'Lala Lajipati Rai', 'Sarojini Naidu', 'Mahatma Gandhi', 'Mahatma Gandhi']

print '---------------Other Functions---------------'
#sorted(freedom_fighters)
#print max(freedom_fighters)
#print min(freedom_fighters)
#print sum(list1)


No comments:

Post a Comment