# B2.2.1 Compare static and dynamic data structures :::info This is a note part of the coding course for IB Computer Science program. If you want to check out other parts of the same course you can see them in [This link of teaching material](/68GDv_RgT-yh9oERMvdnFw) ::: :::warning :warning: Note still under construction :warning: ::: This is mean to be taught after [B2.2.2 Lists in Python](/RWrVgWBOTOebitrb0MSFVw) A static data structure is a structure that once defined (in size) it cannot be changed during runtime. So for example, arrays in Arduino (such as the arrays that we saw in [Sounds with arduino](/I0rOqB99RMGA62qFLcx1Vg)) once they are defined we cannot add more elements. If it's an array of 50 elements it cannot be 48 nor 52. This is not what we have in dynamic data structures such as lists in python ([B2.2.2 Lists in Python](/RWrVgWBOTOebitrb0MSFVw)). Also linked lists, stacks, queues, trees, dictionaries and sets. ## Why not everything dynamic? In short because we may don't have the space in memory or we want to optimize. That's why, for example, Arduino is better used with static data structures because it has very low capacity and when we have dynamic data structures we need intermediate layers (such as the `Garbage collector`) that are going to allocate or deallocate more memory if needed. ### Tuples in Python Tuples in python are a kind of a static data structure that we didn't cover too much. If you don't write any brackets `[]` betwen several values they are going to create a tuple. ```python myTuple = 3, 2, 14 print(myTuple) ``` The thing is that usually in arrays you can modify the internal values of the array, but in a tuple we cannot modify the internal values of the tuple. So it's a bit more static and inmutable than an array. #### References for tuples https://docs.python.org/3/tutorial/datastructures.html#tuples-and-sequences https://ellibrodepython.com/tuplas-python (Spanish) ## Comparasion between static and dynamic data structure (They ask these stuff in the exam) ### Flexibility Dynamic data structures are, of course, more flexible than static. If you have an array you need to set up a size for it and stick to it. In a list you can increase or decrease as the program runs. ### Memory usage The allocation of dynamic data structures tend to be more efficient because you will only need to store the data that the program is using while the static data structures they usually need to allocate for the maximum. Nevetheless it usually comes at a cost because in dynamic data structures you need to allocate more information per node of information. ### Speed Static is slightly better since it tends to be allocated without fragmentation so accessing all the elements is usually easier rather than the fragmentation that can be found in dynamic data structures. ## Exam like questions: Compare a dynamic data structure such as a list in python with a static data structure such as a tuple in python [6] Outline one advantadge of static data structures over dynamic data structures [2] Identify 3 types of dynamic data structures [3]