CS105 Study Guide

Unit 6: Basic Data Structures II – Tuples, Sets, and Dictionaries

6a. Explain the difference between lists and tuples

  • What is a tuple?
  • How are lists and tuples similar?
  • How do lists and tuples differ from one another?

A tuple is an ordered, immutable Python container. This means its elements can be referred to using an index (similar to a list), but once initialized to a set of values, its elements cannot be modified (i.e. they are immutable). This is the main difference between tuples and lists. In other words,

a=[2,3,4]
a[1]=12

is allowed because lists are mutable; however, after creating the tuple

b=(2,3,4)

the instruction

b[1]=12

will generate a TypeError exception because tuples are immutable.

In some sense, tuples allow you to have a list of data that remains constant. Although the syntax for accessing tuple elements is the same as that of a Python list, the set of methods associated with lists and tuples are different (with only a very small degree of overlap). For instance, because of immutability, the append method is not available to use with tuples. Tuples are useful when you want to order data elements so that they can be indexed, but you do not want the data to be changed. For example, if a set of birthdates were to be stored, a tuple could be a sensible choice to house this type of data.

To review, see Tuples and Sets.

 

6b. Explain sets and apply set operations

  • What is a set?
  • How do sets differ from lists and tuples?
  • What is the syntax to create a set?

A set is an unordered, mutable Python container. Mutable means that a set can be modified. Unordered means that elements cannot be accessed using an index. In addition, a set does not contain duplicate elements. The properties associated with different Python built-in containers can be summarized in a table:

Container

Ordered

Mutable

List

yes

yes

Tuple

yes

no

Set

no

yes


An empty set can be initialized using an instruction such as

a=set()

(notice that you should not use a={ } to initialize a set because this indicates an empty dictionary).

A non-empty set can be initialized with an instruction such as

a={3,4,5,6}

After creating a set, the remove and add methods can be useful for removing elements from or adding elements to a set.

Understanding basic set operations such as membership, non-membership, union, intersection and set difference is fundamental to applying Python set operations and set methods. The syntax in and not in can be used to determine set membership. The methods union, intersection, and difference naturally correspond to their associated set operation.

To review, see Tuples and Sets.

 

6c. Create dictionaries using dict

  • What is a dictionary?
  • How do dictionaries differ from lists and sets?
  • What is the syntax to create a dictionary?

A dictionary is an unordered, immutable Python container whose elements consist of key-value pairs. The immutability property needs a bit of clarification as it refers to the key and not the value. Values can be modified, but keys cannot. In addition, key:value pairs can be added to or deleted from a dictionary.

An empty dictionary can be created in one of two ways using an instruction such as:

a=dict()

or

a={ }

In addition, a dictionary can be initialized using an instruction such as:

a={5:22, 'x':34, 'l':'mno'}

Similar to a set, the key:value pairs in a dictionary are not ordered. In addition, the container syntax using curly brackets is similar to a set. Therefore, the usage context must resolve any confusion in the syntax.

Once a key name is chosen, it cannot be modified. This is because the key acts like an index into the dictionary. So, even though elements in a dictionary are not ordered, the key allows for referencing values with square bracket syntax similar to a list or a tuple. For example, values in the dictionary

a={1:'Tom', 2:'Frank', 3:'Robert'}

would be referred to using a[1] (referring to the value 'Tom'), a[2] (referring to the value 'Frank'), or a[3] (referring to the value 'Robert').

Finally, it is important to know how to either add or remove key:value pairs from a dictionary. key:value pairs can be added by using the assignment operator (=) or the update method and they can be removed using the pop method. To summarize, creating dictionaries and modifying their contents is fundamental to applying Python dictionaries.

To review, see Dictionaries.

 

6d. Write programs that apply tuples, sets, and dictionaries

  • How can loops be used with dictionaries, tuples, and sets?
  • How can functions be used with dictionaries, tuples, and sets?
  • How do sets and dictionaries differ from using tuples within a loop?

A major application of using containers is to access and iterate through their elements using a loop. Tuples and lists are ordered, hence, if desired, loops can be iterated based upon their indices. The beauty of Python is that the built-in containers list, tuple, set, and dict can all be iterated upon by referencing their elements. For example, the instruction

for x in a:

could be used to iterate assuming the variable a is a valid list, tuple, set or dictionary. For a dictionary, it must be clear from the context that the iterator variable x would refer to a dictionary key (and not the value). To review, the key acts like an index; therefore, instructions such as

for x in a:
    print(a[x])

would output values from key:value pairs (assuming the variable a is a dictionary).

When writing functions, any valid data type, either built-in or user-defined, can be used as a valid input to or output from a function. This is an important principle for programming languages. Ideally, once the syntax is in place, it should be generalizable, without modification, to all data types. The for loop example above highlights this principle. The same holds true for functions. It is the responsibility of the programmer to understand the context of the data type being used in order to properly apply programming constructs such as loops and functions.

To review, see Dictionaries and Loops.

 

Unit 6 Vocabulary

Be sure you understand these terms as you study for the final exam. Try to think of the reason why each term is included.

  • tuple 
  • immutable
  • index
  • list
  • set
  • mutable
  • containers
  • dictionary
  • key-value pairs