Data Structures in Python

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/29

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

30 Terms

1
New cards

Pros to Complex Types being included Natively

  • Convenient, as you don’t have to implement the data structure or operations yourself

  • Can lead to short and elegant solutions (parsimonious)

  • Can lead to powerful programming paradigms (e.g. list based languages, functional programming paradigms)

2
New cards

Cons to Complex Types being included Natively

  • Harder to understand performance underlying operations

  • Generic implementations are rarely efficient for all tasks (data dependent)

  • Can be harder to control run time behaviour (especially with respect to memory use or bounded resources)

3
New cards

Python Data Types

  • Lists - Ordered, mutable, allows duplicate elements. Useful for sequences of data

  • Tuples - Ordered, immutable, allows duplicate elements. Effectively immutable lists

  • Sets - Unordered, mutable, contains unique elements. Useful for testing membership and eliminating duplicates from e.g. lists

  • Dictionaries - Unordered, mutable, mapped by key-value pairs. Key-value pairs (think hash-table, where we look up the value for a given ‘key’)

4
New cards

Does Order Matter?

Sometimes:

  • Every time we examine the queue the relative order of elements is preserved

  • LIFO (queue) and FIFO (stack), and other ADTs (trees, heaps,…) order matters!

  • For others, the internals are more opaque

5
New cards

Tuples

  • marked by () parentheses, and may have 0 or more values

  • variables are mutable, and can be changed

  • the structure itself is not mutable, you can’t update it’s value

  • accessing the first value in the structure using array subscript notation

<ul><li><p>marked by () parentheses, and may have 0 or more values</p></li><li><p>variables are mutable, and can be changed</p></li><li><p>the structure itself is not mutable, you can’t update it’s value</p></li><li><p>accessing the first value in the structure using array subscript notation</p></li></ul><p></p>
6
New cards

Lists

  • denoted by [], and are mutable

  • can address the list as an array

  • can update/assign values to a list

<ul><li><p>denoted by [], and are mutable</p></li><li><p>can address the list as an array</p></li><li><p>can update/assign values to a list</p></li></ul><p></p>
7
New cards

Data Types in Python are Objects

  • have built in methods

  • includes .append() a value, .extend() with another list

  • can pop() values from it, insert() values within it

  • the list is ordered, so we can build a queue or a stack, and even .sort() it

<ul><li><p>have built in methods</p></li><li><p>includes .append() a value, .extend() with another list</p></li><li><p>can pop() values from it, insert() values within it</p></li><li><p>the list is ordered, so we can build a queue or a stack, and even .sort() it</p></li></ul><p></p>
8
New cards

Data Structures Can do ‘Arithmetic’

knowt flashcard image
9
New cards

Sets

Mutable, but unordered

  • We can’t add sets, but we can union them! (with |)

  • We can |= just like we can +=

  • We can test if an element is in a set

  • The order is not preserved, but uniqueness is

<p>Mutable, but unordered</p><ul><li><p><span>We can’t add sets, but we can union them! (with |)</span></p></li><li><p><span>We can |= just like we can +=</span></p></li><li><p><span>We can test if an element is in a set</span></p></li><li><p><span>The order is not preserved, but uniqueness is</span></p></li></ul><p></p>
10
New cards

Elements

Can be any type, types can be nested

<p>Can be any type, types can be nested</p>
11
New cards

Name Value Pairs

Very useful

myDict = {'Adrian': 'is old', 'Saad': 'is young'}

  • Creates a dictionary with two keys ‘Adrian’, and ‘Saad’

  • Keys and values are both strings in this case

  • myDict[‘Adrian’] returns ‘is old’

  • myDict[‘Joe’] throws a KeyError

<p>Very useful</p><p>myDict = {'Adrian': 'is old', 'Saad': 'is young'}</p><ul><li><p><span>Creates a dictionary with two keys ‘Adrian’, and ‘Saad’</span></p></li><li><p><span>Keys and values are both strings in this case</span></p></li><li><p><span>myDict[‘Adrian’] returns ‘is old’</span></p></li><li><p><span>myDict[‘Joe’] throws a KeyError</span></p></li></ul><p></p>
12
New cards

Functions

an entire data structure

def get_lecturers():
	return {'Adrian' : 18, 'Saad' : 21}

a nested data structure

def get_lectures():
	return [('Adrian', 18), ('Saad', 21)]

something inside a data structure

def get_age(name):
	return {'Adrian' : 18, 'Saad', 21}[name]

multiple values

def get_details():
	return 'Adrian', 18

13
New cards

Assignment

we can assign multiple variables

can pack and unpack data structures into and from variables

the number of variables corresponds to the number of elements we wish to unpack

14
New cards

Multiple Assignment

name, age = 'Saad', 21

15
New cards

Packing

x = 'Adrian', 18

16
New cards

Unpacking

name, age = x

17
New cards

Iterators

iter gets the iterator from an iterable

next gets the next element, if it can

finishes with ‘StopIteration’

<p>iter gets the iterator from an iterable</p><p>next gets the next element, if it can</p><p>finishes with ‘StopIteration’</p>
18
New cards

Using a Loop for Iteration

call the iterator for us

can iterate through a data structure using ‘for’

for lists, sets and tuples this returns each element

<p>call the iterator for us</p><p>can iterate through a data structure using ‘for’</p><p>for lists, sets and tuples this returns each element</p>
19
New cards

Unpacking in Loops

can unpack a dictionary as (key, value) tuples

<p>can unpack a dictionary as (key, value) tuples</p>
20
New cards

filter(function, iterable)

selects items from an iterable that satisfy a condition

input - a function that returns T/F, an iterable

output - iterator with only the items where function returns true

21
New cards

filter Example with Regular Function

grades = [50, 55, 45, 62, 30, 75]

def has_passed(grade):
	return grade >= 50

passed = list(filter(has_passed, grades))
#[50, 55, 62, 75]

22
New cards

filter Example with Lambda Function

grades = [50, 55, 45, 62, 30, 75]

passed = list(filter(lambda grade: grade >= 50, grades))
#[50, 55, 62, 75]

23
New cards

Lambda Function

lambda arguments : expression

small anonymous function

can take any number of arguments, but only one expression

24
New cards

map (function, iterable)

transforms each item in an iterable using a function

input - function to execute for each item and return new item, iterable

output - iterator with transformed items

25
New cards

map Example with Regular Function

passed = [50, 55, 62, 75]

def add_bonus(grade):
	return grade + 5

updated = list(map(add_bonus, passed))
#[55, 60, 67, 80]

26
New cards

map Example with Lambda Function

passed = [50, 55, 62, 75]

updated = list(map(lambda grade: grade + 5, passed))
#OR
updated = [grade + 5 for grade in passed]
#[55, 60, 67, 80]

27
New cards

reduce (function, iterable[, initialiser])

reduces the iterable to a single value by applying the function cumulatively

input - function that takes 2 arguments and performs an operation on them, iterable, optional initialiser as a starting value for the operation

output - single value

28
New cards

reduce Example with Regular Function

numbers = [1, 2, 3, 4, 5]

def add(x, y)
	return x + y

reduced = reduce(add, numbers)
#15

29
New cards

reduce Example with Lambda Function

numbers = [1, 2, 3, 4, 5]

reduced = reduce(lambda x, y: x + y, numbers)
#15

30
New cards

reduce Example with Operator Library Function

numbers = [1, 2, 3, 4, 5]

from operator import add
reduced = reduce(add, numbers)
#15