Data structures: lists, arrays and tables
Apréndelo jugando
Responde estas preguntas para ganar energía, luego pesca y explora. Sin cuenta.
Apuntes de la lección
What are Data Structures?
- A data structure is a way of organising and storing data so it can be used efficiently.
- Common data structures include lists, arrays, and tables.
- Choosing the right structure makes programs simpler and faster.
- In programming, a list is a collection of items in a particular order.
- An array is a type of list where all elements are of the same data type and stored in contiguous memory.
- A table (or two-dimensional array) stores data in rows and columns, like a spreadsheet.
An array is an ordered collection of same-type values stored under one name and reached by index.

Lists and Arrays
- A list is a mutable, linear collection of elements.
- An array is a collection of elements of the same type, each identified by an index.
- Arrays are stored in memory so that the address of each element can be calculated from its index.
- For example, an array of 10 integers starting at address 2000 uses addresses 2000, 2004, 2008, ... (each integer takes 4 bytes).
- The address of element with index i is: base address + (i × size of each element).
- The first element's address is called the base address.
Indexing
- Indexing is how you access individual elements in a list or array.
- Most programming languages use zero-based indexing: the first element is at index 0.
- Some languages (like Lua) use one-based indexing: the first element is at index 1.
- The index is usually a non-negative integer.
- Using the index, you can read or change the value at that position.
Adding and Removing Items
- Lists are mutable, meaning you can add, remove, or change items.
- To add an item, you can use methods like `append()` (add to end) or `insert()` (add at a specific index).
- To remove an item, you can use `remove()` (by value) or `pop()` (by index).
- In arrays, the size is often fixed, but in dynamic lists (like Python lists) the size can change.
- When you add or remove items, the indices of later items may change.
Iterating Over a List
- Iterating means going through each element of a list one by one.
- You can use a for loop to iterate over a list.
- Example in Python: `for item in mylist:` processes each item.
- You can also use a loop with an index: `for i in range(len(mylist)):`.
- Iteration is useful for calculating totals, finding items, or transforming data.
Traversal visits each indexed cell in turn, from the first index to the last.

Two-Dimensional Arrays (Tables)
- A two-dimensional array (or table) is like a grid with rows and columns.
- It is sometimes called a matrix.
- You access an element using two indices: `table[row][column]`.
- Tables are useful for representing data like a spreadsheet, a chessboard, or a timetable.
- You can iterate over a table using nested loops (one for rows, one for columns).
Choosing the Right Structure
- Use a list/array when you have a sequence of items that you need to access by position.
- Use a table when your data naturally has two dimensions, like rows and columns.
- Consider whether you need to add/remove items frequently; lists are flexible, arrays may be fixed-size.
- Think about the data type: arrays usually require all elements to be the same type.
- Choosing the right structure makes your code clearer and more efficient.
Real-World Applications
- Arrays are used to implement vectors and matrices in mathematics.
- Databases often store records in arrays.
- Arrays are used to implement other data structures like stacks, queues, and strings.
- Lookup tables are arrays used to quickly find values.
- Almost every program uses arrays in some way.
Diapositivas
Sign up free to view the lesson slides
Step through every slide for this topic — plus flashcards and revision notes — with a free account.
Preguntas de práctica
Vista previa gratis — 8 de 60 preguntas. Regístrate para verlas todas.
1.What is an array in computer science?
Easy- AA data structure consisting of a collection of elements, each identified by at least one index
- BA type of computer memory that stores programs
- CA programming language used for web development
- DA single variable that can hold only one value
2.In zero-based indexing, the first element of an array has index 1.
EasyTrue or false?
3.Which of the following is a correct way to describe a two-dimensional array?
Easy- AA collection of elements arranged in rows and columns, like a grid
- BA collection of elements all stored in a single row
- CA list that can only store numbers
- DA data structure that cannot be indexed
4.Which of the following are common uses of arrays? (Select all that apply)
Easy- AImplementing mathematical vectors and matrices
- BStoring a single value in a program
- CImplementing other data structures such as lists and strings
- DReplacing all conditional statements in a program
- ECreating lookup tables
5.Which of the following is a reason why arrays are efficient for computers?
Easy- AThey can store elements of different sizes
- BThe position of each element can be computed from its index using a mathematical formula
- CThey do not require memory allocation
- DThey can only be accessed sequentially
6.Match each indexing convention to its description.
Easy- Zero-based indexing
- One-based indexing
- Base address
- First element has index 0
- First element has index 1
- Address of the first element
7.Arrange the following steps to compute the address of an array element using the formula: address = base + (index × size).
Medium- Multiply the index by the size of each element
- Add the result to the base address
- Identify the base address and the size of each element
8.Arrays are used to implement many other data structures such as lists, stacks, and queues.
EasyTrue or false?
Unlock all 60 questions, flashcards & more
Crea una cuenta gratis para ver todas las preguntas, las diapositivas, las tarjetas y los apuntes de repaso de este tema.