Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
83 views

Python Data Structure 4 Updated

This document provides an overview of Python data structures, focusing on lists and tuples. It discusses how to create, access, modify, loop through, sort and copy lists. It also covers list comprehensions for creating new lists from existing lists. For tuples, it explains that they are immutable lists and how to unpack tuples to extract values. The key differences between lists and tuples are that lists are mutable while tuples are immutable.

Uploaded by

denny
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
83 views

Python Data Structure 4 Updated

This document provides an overview of Python data structures, focusing on lists and tuples. It discusses how to create, access, modify, loop through, sort and copy lists. It also covers list comprehensions for creating new lists from existing lists. For tuples, it explains that they are immutable lists and how to unpack tuples to extract values. The key differences between lists and tuples are that lists are mutable while tuples are immutable.

Uploaded by

denny
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 56

ISOM 3400 – Python Programming for Business Analytics

4. Python Data Structure


Yingpeng Robin Zhu

JUN 24, 2022

1
Recap of Last Class

String
 Assignment /Concatenate
 Slicing (e.g., Square brackets [index])
 String Methods (e.g., upper(),lower(),split())
 Modify Strings (e.g., replace())
 Format Strings (e.g., .format())
 Escape Characters (e.g., \”,\’,\n)
 String Comparison/Membership (e.g., ==,!=, in, not in)
 Strings are immutable
Data Structure (List)
 Characteristics (e.g., indexing, changeable, …)

2
Last Labs

Be careful!

3
What is Data Structure?

 Organizing, managing and storing data is important as it enables easier access and
efficient modifications. Data Structures allow you to organize your data in such a way
that enables you to store collections of data, relate them and perform operations on
them accordingly

 In simple terms, data structure refers to the collection or group of data in a particular
structure. In Python, the four common data structures are: Lists, Tuples, Sets, and
Dictionaries

4
What is Data Structure?

5
Lists

 Lists are used to store data of same/different data types in a sequential manner
 There are addresses assigned to every element of the list, which is called as Index
 To create a list, you use the square brackets and add elements into it accordingly

Index 0 1 2 3

Index -4 -3 -2 -1

6
Loop Lists (Continued)

 You can loop through the list items by using a for loop

7
List Comprehension

 List comprehension offers a shorter syntax when you want to create a new list based
on the values of an existing list
 Based on a list of names, you want a new list, containing only the names with the
letter "J" in the name
 Without list comprehension you will have to write a for statement with a conditional
test inside:

8
List Comprehension

 With list comprehension you can do all that with only one line of code:

 The Syntax

The condition is like a filter that only accepts the items that valuate to True.

The iterable can be any iterable object, like a list, tuple, set etc.

The expression is the current item in the iteration, but it is also the outcome, which you can manipulate before 9it
ends up like a list item in the new list
List Comprehension

 Compare the two approaches

10
List Comprehension

 Only to accept names that are not “James”:

The condition if name != “James" will return True for all elements other than “James", making the
new list contain all names except “James".

 We can also omit the condition if, and it will copy the original name list

11
List Comprehension

 The iterable can be any iterable object, like a list, tuple, set etc.
 Try the code below. What is the length of the newlist, and what is the maximum
number in the newlist?

 Accept only numbers lower than 5:

12
List Comprehension

 The expression is the current item in the iteration, but it is also the outcome, which
you can manipulate before it ends up like a list item in the new list

 Set the values in the new list to upper case:

 How can you change the above code to for loop and print the same code?

13
List Comprehension

 The expression can also contain conditions, not like a filter, but as a way to
manipulate the outcome:

 For example, assume we know that the name of "John" is mistakenly written as
"James", and please return "John" instead of "James" if it is "James" :

14
Sort Lists

 Sort List Alphanumerically


 List objects have a sort() method that will sort the list alphanumerically, ascending,
by default
 For a numerical list:

 For a string list:

15
Sort Lists

 Sort List Alphanumerically


 List objects have a sort() method that will sort the list alphanumerically, ascending,
by default
 Can we sort a list that includes both numerical and string value? Try the code and see
what you get:

16
Sort Lists

 Sort List Alphanumerically


 List objects have a sort() method that will sort the list alphanumerically, ascending,
by default
 To sort descending, use the keyword argument reverse = True

17
Copy a List

 Can we copy a list by using string2=string1? Try the code below:

 What if we change the items in List1, say append a new value 6

 How about you print List2 now (what you get from this?)

18
Copy a List

 You cannot copy a list simply by typing list2 = list1, because: list2 will only be a reference
to list1, and changes made in list1 will automatically also be made in list2
 Make a copy of a list with the copy() method

 Another way to make a copy is to use the built-in method list()

19
Tuples

 Tuples are similar to lists, but the only big difference is the elements inside a list can
be changed but in tuple it cannot be changed, so tuple is immutable
 Tuples are written with round brackets ( while Lists with square brackets )

20
Tuples

 Tuples are immutable/unchangeable, meaning that you cannot change, add, or


remove items once the tuple is created
 What if we have to change the items in tuples?
 You can convert the tuple into a list, change the list, and convert the list back into
a tuple:

21
Tuples

 Tuples are immutable/unchangeable, meaning that you cannot change, add, or


remove items once the tuple is created
 Add tuple to a tuple
 You are allowed to add tuples to tuples, so if you want to add one item, (or many),
create a new tuple with the item(s), and add it to the existing tuple:

Note: When creating a tuple with only one item, remember to include a comma after the item, otherwise it
will not be identified as a tuple.

22
Unpacking a Tuple

 When we create a tuple, we normally assign values to it. This is called "packing" a
tuple

 In Python, we are also allowed to extract the values back into variables. This is called
"unpacking"

23
Unpacking a Tuple

 Using Asterisk* (optional)


 If the number of variables is less than the number of values, you can add an * to
the variable name and the values will be assigned to the variable as a list

24
Unpacking a Tuple

 Using Asterisk* (optional)


 What if we change the code to this? Try it to see what you get.

 What conclusion can you get?


 If the asterisk is added to another variable name than the last, Python will
assign values to the variable until the number of values left matches the
number of variables left

25
Built-in tuple function

 tuple.count() function counts the number of specified element that is present in the
tuple
 tuple.index() function returns the index of the specified element. If the elements are
more than one, then the index of the first element of that specified element is
returned

26
Key Difference

 Key difference: immutable vs. mutable


 As lists are mutable, python needs to allocate an extra memory block in case there is
a need to extend the size of the list object after it is created
 In contrary, as tuples are immutable and fixed size, python allocates just the
minimum memory block required for the data
 As a result, tuples are more memory efficient than the lists

27
Tuple vs. List?

 Depends on your needs


 If you have data which is not meant to be changed:
 Using a tuple instead of a list guards against accidental modification
 Tuples are good for describing multiple properties of one unchanging thing.
e.g., the parts of a phone number, the coefficients of a polynomial
 The immutability of a tuple makes it a very lightweight data structure.
Program execution is faster when manipulating a tuple than it is for the
equivalent list
 If you know that the data will grow or shrink during the runtime of the
application, you may need to go with the list data type

28
Python Sets

 Python Sets
 Sets are used to store multiple items in a single variable
 A set is a collection which is unordered, unchangeable*, and unindexed (*set
items are unchangeable, but you can remove items and add new items)
 Sets are written with curly brackets

29
Python Sets

 Python Sets
 Sets do not allow duplicate values, duplicate values will be ignored

 A set can contain different data types

30
Python Sets

 Python Sets
 Sets are not indexed, therefore, items cannot be accessed by indexing

 Using a for loop

31
Add Items in Sets

 Python Sets
 To add one item to a set, use the add() method

 To add items from another set into the current set, use the update() method

32
Remove Items in Sets

 Python Sets
 To remove an item in a set, use the remove(), or the discard() method

33
Remove Items in Sets

 Python Sets
 You can also use the pop() method to remove an item, but this method will
remove the last item. Remember that sets are unordered, so you will not know
what item that gets removed

 The clear() method empties the set

34
Join Sets

 Join Two Sets


 union() method that returns a new set containing all items from both sets

 update() method that inserts all the items from one set into another

Note: Both union() and update() will exclude any duplicate items.
35
Keep ONLY the Duplicates in Sets

 Intersection and Difference


 The intersection() method will return a new set, that only contains the items that
are present in both sets

 The symmetric_difference() method will return a set that contains all items from
either sets, except items that are present in both

36
What are Dictionaries?

 Dictionaries are used to store data values in key: value pairs


 A dictionary is a collection which is ordered, changeable and do not allow duplicates
 Dictionaries are written with curly brackets, and have keys and values

Keys Values

 The dictionary keys can be any immutable data type (i.e., numbers, strings, tuples)
 Dictionary values can be just about any data type (int, lists, functions, strings, etc.)

37
Dictionary Characteristics

 Dictionary is ordered
 Can we use index, i.e., [], to get the item? Try this

 Can we loop through it? Try this and see what you get?

38
Dictionary Characteristics

 Accessing values
 You can access the value of a dictionary by referring to its key name, inside square
brackets

 There is also a method called get() that will give you the same result

39
Dictionary Characteristics

 Get Values
 The values() method will return a list of all the values in the dictionary

40
Dictionary Characteristics

 Get Keys
 The keys() method will return a list of all the keys in the dictionary

41
Dictionary Characteristics

 Get Items
 The items() method will return a list of all the items in the dictionary, as tuples in a
list

42
Dictionary Characteristics

 Check a specified key is present in a dictionary use the in keyword

 Can you also use in to check whether a value is in dictionary or not? Try this

43
Dictionary Characteristics
 Changeable
 We can change, add or remove items after the dictionary has been created
 You can change the value of a specific item by referring to its key name

 Update Dictionary
 The update() method will update the dictionary with the items from the given
argument (to update the value of an existing key)
 The argument must be a dictionary, or an iterable object with key:value pairs

44
Dictionary Characteristics

 Duplicates Not Allowed


 Dictionaries cannot have two items with the same key. If so, duplicate values will
overwrite existing values

45
Dictionary Characteristics

 Add Dictionary Items


 Adding an item to the dictionary is done by using a new index key and assigning a
value to it

 The update() method will update the dictionary with the items from a given
argument (to add new items, both key and value)

46
Dictionary Characteristics

 Remove Items
 The pop() method removes the item with the specified key name

 The popitem() method removes the last inserted item

47
Dictionary Characteristics

 Remove Items
 The del keyword removes the item with the specified key name

 The clear() method empties the dictionary

48
Dictionary Characteristics

 Dictionary Length

 The values in dictionary items can be of any data type


 String, int, Boolean, and list data types

49
Dictionary Characteristics

 Copy a Dictionary
 You cannot copy a dictionary simply by typing dict2 = dict1, because: dict2 will only
be a reference to dict1, and changes made in dict1 will automatically also be made
in dict2
 One way is to use the built-in dictionary method copy()

50
Dictionary Characteristics

 Copy a Dictionary
 You cannot copy a dictionary simply by typing dict2 = dict1, because: dict2 will only
be a reference to dict1, and changes made in dict1 will automatically also be made
in dict2
 Another way is to use the built-in dictionary method dict()

51
Nested Dictionaries

 A dictionary can contain dictionaries, this is called nested dictionaries

52
Nested Dictionaries

 A dictionary can contain dictionaries, this is called nested dictionaries


 You can also add new items to the existing nested dictionary

53
Summary of Dictionary Methods
Method Description
clear() Removes all the elements from the dictionary
copy() Returns a copy of the dictionary
get() Returns the value of the specified key
items() Returns a list containing a tuple for each key value pair
keys() Returns a list containing the dictionary's keys
pop() Removes the element with the specified key
popitem() Removes the last inserted key-value pair
update() Updates the dictionary with the specified key-value pairs
values() Returns a list of all the values in the dictionary

54
Comparison between List and Dictionary

55
Jupyter Notebook

56

You might also like