You are on page 1of 30

IF2106 – Data Engineering

Data Collection Structures (1)


• Python Data Collection Structures
• Lists
• Dictionaries
Undergraduate

Computer Science
Overview

Illustrates data collection structures in Python and their


implementations

Learn how to identify different forms of collection in Python

Learn how to create lists and how to manipulate list content

Learn about the purpose of creating a dictionary as a data container and


its manipulations
Objectives

Upon completion of this Unit, you are expected to be able to:


• Properly build and practice application development using Python
Contents

a. Python Data Collection Structures

b. Lists

c. Dictionaries
Python Data Collection
Structures
Python Data • Lists, dictionaries, tuples, series, and data frames
Collection are Python data collection structures that can be
Structures used to maintain a collection of data
Lists
• A sequence of values of any data type that
can be accessed forward or backward
Lists • Each value is called an element or a list item

• Lists are mutable, which means that you won’t


create a new list when you modify a list
element
• Elements are stored in the given order
• Various operations can be conducted on lists
such as insertion, sort, and deletion
• A list can be created by storing a sequence of different
types of values separated by commas

Lists (Cont.) • A Python list is enclosed between a square brackets


([ ]), and elements are stored in the index based on a
starting index of 0
• You can have lists of string values and integers,
Creating
empty lists, and nested lists, which are lists inside
Lists
other lists
Accessing
Values in Lists
• You can access list
elements forward

• You can also access a list


element backward using
negative indices
Adding and
Updating Lists

• You can update single or


multiple elements of lists by
giving the slice on the left side of
the assign operator

• Also, you can add elements to a


list with the append() method
• To remove a list element, either you can delete
it using the del statement in the element
index, or you can remove the element using
the remove() method via the element value in
the list
Deleting List • If you use the remove() method to remove

Elements an element that is repeated more than


one time in the list, it removes only the
first occurrence of that element inside the
list

• Also, you can use the pop() method to remove


a specific element by its index value
• Like string processing, lists respond to + and *
Basic List
operators as concatenation and repetition,
Operations
except that the result is a new list
Indexing, Slicing, and Matrices

Lists are a sequence of indexed elements that can be


accessed forward or backward

Therefore, you can read their elements using a positive index


or negative (backward) index
Built-in List • Various functions and methods can be used
Functions and
Methods for list processing
List Sorting and Traversing

• Sorting lists is important, especially for list-searching purposes

• You can create a list from a sequence; in addition, you can sort
and traverse list elements for processing using iteration
statements
Lists and Strings

• You can split a string into a list of characters

• In addition, you can split a string into a list of words using the split()
method

• The default delimiter for the split() method is a white space


• However, you can specify which characters to use as the word boundaries
• For example, you can use a hyphen as a delimiter
Lists and Strings (Cont.)

• The join() method is the inverse of the split


method
• It takes a list of strings and concatenates
the elements

• You have to specify the delimiter that the


join() method will add between the list
elements to form a string
Aliasing

• The assign operator is dangerous if you don’t


use it carefully

• The association of a variable with an object is


called a reference

• In addition, an object with more than one


reference and more than one name is called
an alias
Dictionaries
Dictionaries

• An unordered set of key-value pair; each key is separated from its


value by a colon (:)
• The items (the pair) are separated by commas, and the whole thing is
enclosed in curly braces ({ })

• In fact, an empty dictionary is written only with curly braces

• Dictionary keys should be unique and should be of an immutable data


type such as string, integer, etc.
Dictionaries (Cont.)

• Dictionary values can be repeated many


times, and the values can be of any data
type

• It’s a mapping between keys and values;


you can create a dictionary using the dict()
method
Creating
Dictionaries
• You can create a
dictionary and assign a
key-value pair directly

• In addition, you can create


an empty dictionary and
then assign values to
each generated key
Once you have created a
dictionary, you can update
and access its values for any
Updating
further processing and
Accessing
You can directly access any
element in the dictionary or
Values in
iterate
elements
all dictionary Dictionaries
Deleting Dictionary Elements

• You can either remove individual dictionary elements using the


element key or clear the entire contents of a dictionary

• Also, you can delete the entire dictionary in a single operation


using a del keyword

• It should be noted that it’s not allowed to have repeated keys


in a dictionary
Built-in Dictionary
• Various built-in functions and methods can
Functions and
Methods be implemented on dictionaries
Differences Between Lists and Dictionaries
Summary

This Unit covered data collection structures in Python and their


implementations. Here’s a recap of what was covered in this Unit:

• How to maintain a collection of data in different forms

• How to create lists and how to manipulate list content

• What a dictionary is and the purpose of creating a dictionary as a data


container
Discussion

You might also like