More on Iterable Data Types

Overview

Teaching: 10 min
Exercises: 0 min
Questions
  • What do sequences, sets, and dictionaries have in common?

  • How do they differ?

  • When should I use each type?

Objectives
  • Explain what it means for a data type to be iterable.

  • Understand the difference between ordered and unordered types.

  • Explain how a mutable data type differs from an immutable data type.

  • Understand when to use one iterable type versus another.

What is an iterable data type?

What is iteration?

Example

  • Translating a collection of DNA sequences into protein sequences
  • Get the atomic number of every element in a molecule
  • Calculate the mean of 3 measurements at every timepoint

What are the iterable data types?

Ordered vs. Unordered Types

Try running the following:

my_list = list('abc')
my_set = set('abc')

print('my_list = ', my_list)
print('my_set = ', my_set)

What do you notice about the result of printing my_list and my_set?

Solution

my_list = ['a', 'b', 'c']
my_set = {'b', 'c', 'a'}

Challenge

  • Is unordered the same as random?
  • Will the same thing print each time?
print(set('abc'))
print(set('bca'))
print(set('cba'))

Solution

{'b', 'c', 'a'}
{'b', 'c', 'a'}
{'b', 'c', 'a'}

Unordered means you can’t depend on any particular order

  • The order may change when you upgrade python
  • It may also change the next time you run your program

Mutable vs Immutable Types

Example

Run the following code. What’s the difference?

my_list = ['a', 'b', 'c']
also_my_list = my_list

my_string = 'abc'
also_my_string = my_string

my_list.extend(['d', 'e', 'f'])
my_string = my_string + "def"

print('my_list = ', my_list)
print('also_my_list = ', also_my_list)

print('my_string = ', my_string)
print('also_my_string = ', also_my_string)

Solution

  • Modifying my_list also modifies also_my_list
  • Modifying my_string does not change also_my_string
my_list =  ['a', 'b', 'c', 'd', 'e', 'f']
also_my_list =  ['a', 'b', 'c', 'd', 'e', 'f']
my_string =  abcdef
also_my_string =  abc

Iterables have lengths

print(len("abc"))
print(len([1, 2, 3, 4]))
3
4
print(len(52))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-f769e8e8097d> in <module>()
----> 1 print(len(52))

TypeError: object of type 'int' has no len()

Choosing the best data structure

Lists & Tuples vs Dictionaries

Lists vs Tuples

Comparing Different Data Structures

Data Structure Used When/For Notes
List Simple Collection of Elements
  • Has a consistent order
  • Finding may require inspecting all elements
Set Unique collection of elements
  • Has random order
  • Finding items will be fast
Tuple Immutable collection of elements
  • All elements in collection must be present at creation
  • Immutability is useful to protect data from unintended changes
Dictionary Collections indexed by keys

Can encode simple object-like data structures
  • Has random order
  • Lookup by key will be fast
  • Simple data structures behave like temporary in-memory databases

Key Points

  • Iterable data types are collections of objects.

  • Ordered data types perserve the order of creation.

  • Unordered data types return data in a random order.

  • Mutable data types can be changed in place.

  • Immutable data types cannot be changed in place. To modify them, you must create a new object.

  • All iterable types have a length, the number of items they hold

  • Choose an iterable type that makes your code safer and easier to understand.