Tuples & Dictionaries

Overview

Teaching: 10 min
Exercises: 0 min
Questions
  • How can I add semantic value to my collections of multiple values?

Objectives
  • Identify lists, tuples and dictionaries

  • Write programs that create and modify dictionaries

  • Select the best data collection type for a given purpose.

Tuples are “immutable” lists

name = ('Paul', 'Wilson')
first_name = name[0]
last_name = name[1]
print(first_name,last_name)
name = ('Paul', 'Wilson')
name[1] = 'Nagus-Wilson'
TypeError                                 Traceback (most recent call last)
<ipython-input-85-8f02ace9e6af> in <module>()
      1 name = ('Paul','Wilson')
----> 2 name[1] = 'Nagus-Wilson'

TypeError: 'tuple' object does not support item assignment

Immutability preserves order

first_names = ['Paul', 'Patrick', 'Sarah']
last_names = ['Wilson', 'Shriwise', 'Stevens']
last_names[1] = 'Nagus-Wilson'
print(first_names)
print(last_names)
names = [('Paul','Wilson'), ('Patrick','Shriwise'), ('Sarah','Stevens')]
print(names[0])

Dictionaries add meaningful labels

name = {'first': 'Paul', 'last': 'Wilson'}
print(name['first'], name['last'])
name['last'] = 'Nagus-Wilson'
print(name['first'], name['last'])
Paul Wilson
Paul Nagus-Wilson

Entries can be added to a dictionary

my_name = {'first': 'Paul', 'last': 'Wilson'}
my_name['middle'] = 'Philip'
print(my_name)
my_name['last'] = 'Nagus-Wilson'
print(my_name)
your_name['first'] = 'Henry'
{'first': 'Paul', 'last': 'Wilson', 'middle': 'Philip'}
{'first': 'Paul', 'last': 'Nagus-Wilson', 'middle': 'Philip'}
--------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-97-cc04cabb3603> in <module>()
      1 my_name = {'first': 'Paul', 'last': 'Wilson'}
      2 my_name['middle'] = 'Philip'
----> 3 your_name['first'] = 'Henry'

NameError: name 'your_name' is not defined

Use del to remove items from a dictionary entirely.

my_name = {'first': 'Paul', 'middle': 'Philip', 'last': 'Wilson'}
print(my_name)
del my_name['middle']
print(my_name)
{'first': 'Paul', 'middle': 'Philip', 'last': 'Wilson'}
{'first': 'Paul', 'last': 'Wilson'}

Various ways to access all the entries in a dictionary

name = {'first': 'Paul', 'last': 'Wilson'}
print(name.keys())
print(name.values())
dict_keys(['first', 'last'])
dict_values(['Paul', 'Wilson'])

All collection types can be nested

instructors = [{'first':'Paul', 'middle': 'Philip', 'last':'Wilson'},       # list of dictionaries
               {'first':'Sarah','last':'Stevens'},
               {'first':'Patrick','last':'Shriwise'}]
axes_tics = {'x': [0,0.1,0.2,0.3,0.4,0.5],              # dictionary of lists
             'y': [0,10,20,30,40,50]}
curve_points = [(0,0), (2,3), (4,5), (6,-3), (8,-10)]   # list of tuples
shapes = { 'shape_01': [(0,0), (3,4), (6,-3), (10,-6)], # dictionary of lists of tuples
           'other_shape': [(12,4.5), (15,10), (4,78)] }
print(shapes)

Choosing the best data structure

Lists & Tuples vs Dictionaries

Lists vs Tuples

Key Points

  • A tuple is a list that can never be changed.

  • Use an item’s index to fetch it from a tuple.

  • Dictionaries are lists in which the semantic meaning of each entry is more important than its order.

  • New entries can be added to an existing dictionary by assignment.

  • Dictionary keys are unique - only one item can exist per key

  • Use del to remove items from a list entirely

  • You can access all the keys and all the values of a dictionary

  • Collections (lists, tuples, dictionaries) may be nested arbitrarily

  • Code clarity & maintainability should guide choice of collection type