Data structure
object in program that holds a collection of data
array
holds components of a vector, or a list of names
Why is it useful to use lists?
we can perform operations on the list, like checking length, sorting names and editing it
nested list
list inside a list
list
sequence of data, constructed using square brackets
len
returns the length of the list
enumerate
iterate over a list and get position of each member in the list
How does counting differ in python?
Starts from 0
append
add elements to a list
indexing
assigning number to element
indices start from 0 and run to (length-1)
Why is it better to iterate rather than index?
there are data structures that support iterations but which do not support indexing
Heterogeneous data structure
Store mixed types
python lists are an example
Homogenous data structures
store only one type
arrays are an example - more efficient generally
list comprehension
succinctly built lists from other lists
What is the difference between a tuple and list?
It is a list that cannot be changed after it has been created - it is immutable
Tuples also use round brackets
Why is it useful to see a list that cannot be changed?
safer since cannot be accidentally modified in the program
permits implementations to possibly exploit this to optimise for speed
dictionary
Can build a map from keys and values
Each entry separated by comma
How do you declare a dictionary
Curly brackets
Entry separated by comma
Why is choosing the right data structure an important decision in programming
Affects the program’s efficiency, memory usage, flexibility, and ease of development
What is the general trade-off between flexible and simple data structures?
More flexible data structures are usually less efficient and may use more memory than simpler ones
What should you prioritise when efficiency is not critical?
Choose a data structure that provides the needed functionality, flexibility and ease of use
Why should safety be considered when choosing a data structure?
Some data structures prevent modification, reducing the risk of errors
Why should you use iterators rather than indexing
Allows switching from data structures that support indexing to data structures that do not support indexing