Python Lists and Dictionaries

I Lists

i. Introduction to Lists 1/14

zoo_animals = ["pangolin", "cassowary", "sloth", "tiger"];
# One animal is missing!

if len(zoo_animals) > 3:
	print "The first animal at the zoo is the " + zoo_animals[0]
	print "The second animal at the zoo is the " + zoo_animals[1]
	print "The third animal at the zoo is the " + zoo_animals[2]
	print "The fourth animal at the zoo is the " + zoo_animals[3]

ii. Access by Index 2/14

numbers = [5, 6, 7, 8]

print "Adding the numbers at indices 0 and 2..."
print numbers[0] + numbers[2]
print "Adding the numbers at indices 1 and 3..."
print numbers[1] + numbers[3]

iii. New Neighbors 3/14

zoo_animals = ["pangolin", "cassowary", "sloth", "tiger"]
# Last night our zoo's sloth brutally attacked 
#the poor tiger and ate it whole.

# The ferocious sloth has been replaced by a friendly hyena.
zoo_animals[2] = "hyena"
zoo_animals[3] = "lion"
# What shall fill the void left by our dear departed tiger?
# Your code here!

II List Capabilities and Functions

i. Late Arrivals & List Length 4/14

suitcase = [] 
suitcase.append("sunglasses")

# Your code here!
suitcase.append("doll")
suitcase.append("ball")
suitcase.append("comb")


list_length = len(suitcase) # Set this to the length of suitcase

print "There are %d items in the suitcase." % (list_length)
print suitcase

ii. List Slicing 5/14

suitcase = ["sunglasses", "hat", "passport", "laptop", "suit", "shoes"]

first  = suitcase[0:2]  # The first and second items (index zero and one)
middle = suitcase[2:4]   # Third and fourth items (index two and three)
last   = suitcase[4:6]  # The last two items (index four and five)

iii. Slicing Lists and Strings 6/14

animals = "catdogfrog"
cat  = animals[:3]   # The first three characters of animals
dog  = animals[3:6]          # The fourth through sixth characters
frog = animals[6:10]              # From the seventh character to the end

iv. Maintaining Order 7/14

animals = ["aardvark", "badger", "duck", "emu", "fennec fox"]
duck_index = animals.index("duck")
# Use index() to find "duck"
animals.insert(duck_index,"cobra")# Your code here!
print animals # Observe what prints after the insert operation

v. For One and All 8/14

my_list = [1,9,3,8,5,7]

for number in my_list:
    print 2*number
      # Your code here

vi. More with ‘for’ 9/14

start_list = [5, 3, 1, 2, 4]
square_list = []

for x in start_list:
    
    square_list.append(x**2)
    square_list.sort()
print square_list

III Dictionaries

i. This Next Part is Key 10/14

# Assigning a dictionary with three key-value pairs to residents:
residents = {'Puffin' : 104, 'Sloth' : 105, 'Burmese Python' : 106}

print residents['Puffin'] # Prints Puffin's room number

print residents['Sloth']
print residents['Burmese Python']
# Your code here!

ii. New Entries 11/14

menu = {} # Empty dictionary
menu['Chicken Alfredo'] = 14.50 # Adding new key-value pair
print menu['Chicken Alfredo']

menu['Mom Alfredo'] = 10.50
menu['Bob Alfredo'] = 13.50
menu['Fish Alfredo'] = 19.50
menu['Lamb Alfredo'] = 17.50 # Your code here: Add some dish-price pairs to menu!

print "There are " + str(len(menu)) + " items on the menu."
print menu

iii. Changing Your Mind 12/14

# key - animal_name : value - location 
zoo_animals = { 'Unicorn' : 'Cotton Candy House',
'Sloth' : 'Rainforest Exhibit',
'Bengal Tiger' : 'Jungle House',
'Atlantic Puffin' : 'Arctic Exhibit',
'Rockhopper Penguin' : 'Arctic Exhibit'}
# A dictionary (or list) declaration may break across multiple lines

# Removing the 'Unicorn' entry. (Unicorns are incredibly expensive.)
del zoo_animals['Unicorn']
del zoo_animals['Sloth']
del zoo_animals['Bengal Tiger']
zoo_animals['Rockhopper Penguin'] = 'Antarctic Exhibit'

# Your code here!
print zoo_animals

iv. Remove a Few Things 13/14

backpack = ['xylophone', 'dagger', 'tent', 'bread loaf']
backpack.remove('dagger')

v. It’s Dangerous to Go Alone! Take This 14/14

inventory = {
    'gold' : 500,
    'pouch' : ['flint', 'twine', 'gemstone'], # Assigned a new list to 'pouch' key
    'backpack' : ['xylophone','dagger', 'bedroll','bread loaf']
}

# Adding a key 'burlap bag' and assigning a list to it
inventory['burlap bag'] = ['apple', 'small ruby', 'three-toed sloth']
inventory['pocket'] = ['seashell','strange berry','lint']

# Sorting the list found under the key 'pouch'
inventory['pouch'].sort() 
inventory['backpack'].sort()
inventory['backpack'].remove('dagger')
inventory['gold'] = 550
Advertisements

One thought on “Python Lists and Dictionaries

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