Introduction to Computer Science Through Programming
Smith Computer Science
        
      
With lists, we mentioned a couple of ways of going over the contents:
li = ['a', 'b', 'c', 'd', 'e'] for idx in range(len(li)): print(li[idx])
li = ['a', 'b', 'c', 'd', 'e'] for elem in li: print(elem)
li = ['a', 'b', 'c', 'd', 'e'] idx = 0 for elem in li: print(idx, elem) idx += 1
li = ['a', 'b', 'c', 'd', 'e'] for count, value in enumerate(li): print(count, value)
        A Tuple is created ike this:
        
player_tuple = ('Pablo', 'Wizard') print(player_tuple)
print(player_tuple[1])
        Dictionaries are like lists but instead of accessing elements by their index, we access them through a key.
        
        You can read about them in the Python 2 API for Dictionaries
        
        
        
        
# braces MLB_team_1 = { 'Colorado':'Rockies', 'Boston': 'Red Sox', 'Minnesota': 'Twins', 'Milwaukee': 'Brewers', 'Seattle': 'Mariners' } print (MLB_team_1)
# list of pairs MLB_team_2 = dict ([ ('Colorado', 'Rockies'), ('Boston', 'Red Sox'), ('Minnesota', 'Twins'), ('Milwaukee', 'Brewers'), ('Seattle', 'Mariners') ]) print (MLB_team_2)
# assigned pairs MLB_team_3 = dict ( Colorado = 'Rockies', Boston = 'Red Sox', Minnesota = 'Twins', Milwaukee = 'Brewers', Seattle = 'Mariners' ) print (MLB_team_3)
MLB_team_1 = { 'Colorado':'Rockies', 'Boston': 'Red Sox', 'Minnesota': 'Twins', 'Milwaukee': 'Brewers', 'Seattle': 'Mariners' } print(MLB_team_1['Milwaukee'])
print (MLB_team_1[1])
# empty dictionary bag = {}
MLB_team_1['New York'] = 'Mets' print (MLB_team_1)
# creation (empty) players = {} # adding players['Pablo'] = 'Wizard' players['Yak'] = 'Barbarian' players['Mar'] = 'Rogue' players['Dan'] = 'Priest' print (players)
# creation (empty) players = {} # adding players['Pablo'] = 'Wizard' players['Yak'] = 'Berzerker' players['Mar'] = 'Rogue' players['Dan'] = 'Priest' print (players) print (players['Shinyoung'])
        The following table shows a sumary of common methods for dictionaries:
        
        
        
        In particular, look at the get method.
        
        It allows you to deal with nonexistent keys. Later, we'll see another way of dealing with these types of runtime exceptions.
      
players = {} players['Pablo'] = 'Wizard' players['Yak'] = 'Berzerker' players['Mar'] = 'Rogue' players['Dan'] = 'Priest' for player in players: print(player)
players = {} players['Pablo'] = 'Wizard' players['Yak'] = 'Berzerker' players['Mar'] = 'Rogue' players['Dan'] = 'Priest' # items view print("### items view ###") p_items = players.items() for player, role in p_items: print ("player {} is a {}".format(player, role)) print() # keys view print("### keys view ###") p_keys = players.keys() for player in p_keys: print ("one player is {}".format(player)) print() # values view print("### values view ###") p_values = players.values() for role in p_values: print ("one role is {}".format(role))
players = {} players['Pablo'] = 'Wizard' players['Yak'] = 'Berzerker' players['Mar'] = 'Rogue' players['Dan'] = 'Priest' # converting to list pk_list = list(players.keys()) # sorting that list sorted_pk_list = sorted(pk_list) print(sorted_pk_list)
players = {} players['Pablo'] = 'Wizard' players['Yak'] = 'Berzerker' players['Mar'] = 'Rogue' players['Dan'] = 'Priest' # converting to list pi_list = list(players.items()) # sorting that list sorted_pi_list = sorted(pi_list) print(sorted_pi_list)
players = {} players['Pablo'] = 'Wizard' players['Yak'] = 'Berzerker' players['Mar'] = 'Rogue' players['Dan'] = 'Priest' # converting to list pi_list = list(players.items()) # sorting that list sorted_pi_list = sorted(pi_list) print(sorted_pi_list) for key, value in pi_list: print ("player {} is a {}".format(key, value))
players = {} players['Pablo'] = 'Wizard' players['Yak'] = 'Berzerker' players['Mar'] = 'Rogue' players['Dan'] = 'Priest' # converting to list p_list = list(players) # sorting that list sorted_p_list = sorted(p_list) print(sorted_p_list)
        Check out this article on working with Dictionaries
        
        Also, check this article on enumerate