Python Collections, Tuple, Dictionary and List simple example


You can check the following code piece in order to  have basic knowledge regarding Python collections;

#Created on Jul 26, 2013
#@author: tunatore

#python collections

#tuple example which is generally not mutable
animals = ('Cat','Dog','Elephant','Bird','Tiger','Lion',\
 'Rabbit')

#tuples are accessed by index and similar to list
print ("this will print", animals[0]) #index starts from 0
print ("this will print", animals[6])

#IndexError: tuple index out of range
#print (animals[7])

listAnimals = ['','Goose','Bison','Pelican']

#list access
print ("list print", listAnimals[0])
print ("list print", listAnimals[1])
print ("list print", listAnimals[3])

#list difference, you can change lists
listAnimals.append('Whale')
print ("list print", listAnimals[4])

#add Whale again to list
listAnimals.append('Whale')
print ("list print", listAnimals[5])

#count Whale element in List

print ("list Whale count is", listAnimals.count('Whale'))

#remove Whale element first occurrence
listAnimals.remove('Whale')

print ("list Whale count is", listAnimals.count('Whale'))

#list total elements count
print("Count Elements in listAnimals", len(listAnimals))

#you can also count tuples with len
print("Count Elements in animals Tuple", len(animals))

#before list sort
print ("before sort", listAnimals[1])
print ("before sort", listAnimals[2])
print ("before sort", listAnimals[3])

listAnimals.sort()

#after list sort
print ("after sort", listAnimals[1])
print ("after sort", listAnimals[2])
print ("after sort", listAnimals[3])

#python dictionary example
dictionaryAnimalSpeed = {'tiger': 65, 'monkey': 25 ,'bison': 35, 'cheetah': 113 }

#accessing dictionary with key
print ("monkey's speed -",dictionaryAnimalSpeed['monkey'])
print ("tiger's speed -",dictionaryAnimalSpeed['tiger'])

#you can even change tiger's speed
dictionaryAnimalSpeed['tiger'] = 71
print ("new tiger's speed -",dictionaryAnimalSpeed['tiger'])
#adding new element
dictionaryAnimalSpeed['cow'] = 25

#getting dictonary keys
animalNamesInDictionary = dictionaryAnimalSpeed.keys()
for animal in animalNamesInDictionary:
 print ("animal name ", animal)

#ordered dictionary

import collections
print ("\nordered dictionary")
orderedDictionary = collections.OrderedDict(sorted(dictionaryAnimalSpeed.items()))
for key, value in orderedDictionary.items():
 print (key, value)

this will print Cat
this will print Rabbit
list print
list print Goose
list print Pelican
list print Whale
list print Whale
list Whale count is 2
list Whale count is 1
Count Elements in listAnimals 5
Count Elements in animals Tuple 7
before sort Goose
before sort Bison
before sort Pelican
after sort Bison
after sort Goose
after sort Pelican
monkey’s speed – 25
tiger’s speed – 65
new tiger’s speed – 71
animal name cheetah
animal name tiger
animal name monkey
animal name bison
animal name cow

ordered dictionary
bison 35
cheetah 113
cow 25
monkey 25
tiger 71

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s