COMP-122
INTRODUCTION TO COMPUTER
PROGRAMMING
Lecture 7: Collective Data Types
OUTLINE
• Introduction to lists
• Slicing in lists
• Built-in list functions
• List comprehension
• Multidimensional lists
• Lists vs tuples
• Dictionaries
INTRODUCTION TO LISTS
• A list is a collection of items in a particular order. In other
words, a list is a value that contains multiple values in an
ordered sequence.
• You can put anything you want into a list, and the items in
your list don’t have to be related in any way.
• A list begins with an opening square bracket and ends with
a closing square bracket, [].
• Values inside the list are also called items. Items are
separated with commas.
• A list value looks like this: ['cat', 'bat', 'rat', 'elephant']
CREATING LISTS
• There are several ways to create a new list; the simplest
is to enclose the elements in square brackets ([ and ]):
• [10, 20, 30, 40]
• ['blantyre', 'mzuzu', 'lilongwe’]
• The elements of a list don’t have to be the same type.
• A list that contains no elements is called an empty list;
you can create one with empty brackets, [].
TRAVERSING LISTS
• Lists are ordered collections, so you can access any
element in a list by telling Python the position, or index,
of the item desired. To access an element in a list, write
the name of the list followed by the index of the item
enclosed in square brackets.
>>> districts = ['zomba', 'balaka', 'ntcheu']
>>> print(districts[0])
SLICES IN LISTS
• Just as an index can get a single value from a list, a slice can
get several values from a list, in the form of a new list.
• A slice is typed between square brackets, like an index, but it
has two integers separated by a colon.
• Notice the difference between indexes and slices.
• spam[2] is a list with an index (one integer).
• spam[1:5] is a list with a slice (two integers).
CHANGING VALUES IN A LIST
• You can also use an index of a list to change the value
at that index
IN-OPERATOR
• You can determine whether a value is or isn’t in a list with the in and
not in operators.
• Like other operators, in and not in are used in expressions and
connect two values: a value to look for in a list and the list where it may
be found.
• These expressions will evaluate to a Boolean value.
• Example:
BUILT-IN LIST FUNCTIONS
• All examples are using spam = [‘cat’, ‘dog’,’bat’];
Function Syntax Example Output
append LIST.append(‘valu spam.append(‘moo spam = [‘cat’, ‘dog’, ’bat’, ’moose’]
e’) se’)
insert LIST.insert(index,’ spam.insert(1, spam = [‘cat, ‘chicken’,’dog’,’bat’]
value) ‘chicken’)
remove LIST.remove(‘valu spam.remove(‘bat’) spam= [‘cat’,’dog’]
e’)
del del del spam[0] spam = [‘dog’,’bat’]
LIST_with_index
pop LIST.pop(position) spam.pop(1) spam= [‘cat’,’bat’]
sort LIST.sort() spam.sort() spam = [‘bat’,’cat’,’dog’]
len len(LIST) len(spam) 3
LIST COMPREHENSION
• List comprehension offers a shorter syntax when you want to create a new list
based on the values of an existing list.
• Syntax:
• Example: Based on a list of fruits, you want a new list, containing only the
fruits with the letter "a" in the name.
LIST COMPREHENSION (CONTD)
• Example: Based on a list of fruits, you want a new list, containing only the
fruits with the letter "a" in the name.
MULTIDIMENSIONAL LISTS
• A list within another list is nested. There can be more than one
additional dimension to lists in Python. Keeping in mind that a list can
hold other lists, that basic principle can be applied over and over. Multi-
dimensional lists are the lists within lists.
LISTS VS TUPLES
• Lists work well for storing sets of items that can change throughout the
life of a program.
• However, sometimes you’ll want to create a list of items that cannot
change. That is where tuples come in.
• Python refers to values that cannot change as immutable, and an
immutable list is called a tuple.
• A tuple looks just like a list except you use parentheses instead of
square brackets.
• For example, if we have a rectangle that should always be a certain
size, we can ensure that its size doesn’t change by putting the
dimensions into a tuple:
DICTIONARY
• A dictionary is a key:value pair, similar to an associative array found in
other programming languages.
• A dictionary is like an address-book where you can find the address or
contact details of a person by knowing only his/her name i.e. we
associate keys (name) with values (details).
• Note that the key must be unique just like you cannot find out the
correct information if you have two persons with the exact same name.
• Unlike tuples, dictionaries are mutable.
TRAVERSING DICTIONARIES
The methods keys(), values() and items() are used to get views as
displayed below. A for-loop can be used to access single elements of
the views.
TRAVERSING DICTIONARIES
(CONTD)
DICTIONARY RESTRICTIONS
• First, a given key can appear in a dictionary only once. Duplicate keys are
not allowed.
• Secondly, a dictionary key must be of a type that is immutable. A tuple
can also be a dictionary key because tuples are immutable.
• Thus, neither a list nor another dictionary can serve as a dictionary key,
because lists and dictionaries are mutable.
• By contrast, there are no restrictions on dictionary values.
Literally none at all. A dictionary value can be any type of object
Python supports, including mutable types like lists and
dictionaries.
• There is also no restriction against a particular value appearing
in a dictionary multiple times.
LISTS VS DICTIONARIES
• The main difference is items in dictionaries are accessed via their keys
and not positions.