Module04-Lists Tuples Sets and Dictionaries
Module04-Lists Tuples Sets and Dictionaries
binnur.kurt@rc.bau.edu.tr
MODULE 4
LISTS, TUPLES, SETS, AND DICTIONARIES
TUPLE
Tuple
> Tuples are immutable and typically store heterogeneous data, but the data can
be homogeneous.
> A tuple’s length is its number of elements and cannot change during program
execution.
Tuple
Tuple
LIST
List
Empty List
Creating a List
Creating a List
Creating a List
Creating a List
List
> Lists , like tuples, are sequences that contain elements referenced starting at
zero.
> The individual elements of a list can be accessed in the same way as tuples.
List
> You reference a list element by writing the list’s name followed by the
element’s index enclosed in square brackets
Sequence Slicing
Slicing with Steps
> The following code uses a step of 2 to create a slice with every other element
of numbers
> You can use a negative step to select slices in reverse order.
> The following deletes only the first three elements of numbers by assigning an
empty list to the three-element slice:
Modifying Lists Via Slices
> The following assigns a list’s elements to a slice of every other element of
numbers:
del Statement
> The del statement also can be used to remove elements from a list and to
delete variables from the interactive session.
> You can remove the element at any valid index or the element(s) from any valid
slice.
> Let’s create a list, then use del to remove its last element:
del Statement
> The following deletes the list’s first two elements:
> The following uses a step in the slice to delete every other element from the
entire list:
del Statement
> The following code deletes all the list’s elements:
Sorting a List in Ascending Order
> A common computing task called sorting enables you to arrange data either in
ascending or descending order.
> List method sort modifies a list to arrange its elements in ascending order
Key Functions
> Both list.sort() and sorted() have a key parameter to specify a function to be
called on each list element prior to making comparisons.
Key Functions
Searching Sequences
> Often, you’ll want to determine whether a sequence (such as a list, tuple or
string) contains a value that matches a key value.
> Searching is the process of locating a key.
> List method index takes as an argument a search key—the value to locate in
the list—then searches through the list from index 0 and returns the index of
the first element that matches the search key:
Searching Sequences
Set
> A set is an unordered collection of unique values.
> Sets may contain only immutable objects, like strings, ints, floats and tuples
that contain only immutable elements.
> Sets are iterable
> They are not sequences and do not support indexing and slicing with square
brackets, [].
– Dictionaries also do not support slicing.
Set
> The following code creates a set of strings named colors:
> Notice that the duplicate string 'red' was ignored without causing an error
Comparing Sets
> The < operator tests whether the set to its left is a proper subset of the one to
its right
Mathematical Set Operations: Union
> The union of two sets is a set consisting of all the unique elements from both
sets.
> You can calculate the union with the | operator or with the set type’s union
method:
Dictionary
> A dictionary is like lists and tuples.
> It is another type of container for a group of data.
> Lists are indexed by their numeric order
> Dictionaries are indexed by names that you choose.
> These names can be letters, numbers, strings, or symbols — whatever suits
you.
Unique Keys
> A dictionary’s keys must be immutable (such as strings, numbers or tuples) and
unique (that is, no duplicates).
> Multiple keys can have the same value, such as two different inventory codes
that have the same quantity in stock.
Dictionary
Dictionary
Dictionary
Dictionary
Dictionary
Converting Dictionary Keys, Values, and Pairs to Lists