levo

Data Structures

Lists and tuples, and creating, traversing, adding and removing items

  • 2 things to know
  • Component 01

Overview

Recall questions on lists and tuples and how they differ from arrays, immutability, when to choose a tuple, and how to create, traverse, add to and remove from arrays, records and lists.

What you need to know

Answer each one before you reveal it.

Your recallNot started
Key term

What is a list?

Key term

How do you add an item to an array, and to a list?

Questions from this subtopic

Each one with the model answer underneath.

  1. 1

    What is a list?

    Model answer

    An ordered, dynamic collection of items where each item has a position. Unlike an array, a list can grow and shrink at run time, items can be inserted and removed at any position, and in many languages the items do not all have to be the same data type. Underneath it may be implemented with a dynamic array or a linked list.

  2. 2

    Give two ways a list differs from an array.

    Model answer

    A list is dynamic - it can grow and shrink while the program runs - whereas an array's size is fixed on creation. A list also usually provides built-in operations for inserting and removing at any position and in many languages may hold mixed data types, whereas an array typically holds a single type and requires you to shuffle elements yourself.

  3. 3

    What is a tuple?

    Model answer

    An ordered set of values of fixed length, which is immutable - once it has been created its contents cannot be changed. Tuples are usually written in parentheses, for example (53.4, -2.9) for a coordinate pair, and their values may be of different data types.

  4. 4

    Tuples are immutable and lists are not. Explain exactly what that means in practice.

    Model answer

    Immutable means that once a tuple exists you cannot alter it: you cannot change the value at a position, you cannot add a new item to it, and you cannot delete an item from it. A list is mutable, so all three of those operations are allowed. To 'change' a tuple you must build a new tuple from the old one's values and assign that instead - the original is never modified. This mutable/immutable distinction is the examinable difference between the two.

  5. 5

    Why would you deliberately choose a tuple rather than a list?

    Model answer

    When the data is a fixed group of values that should never change, immutability protects it from accidental modification and makes the programmer's intent clear. Tuples are therefore good for coordinates, RGB colour triples, a date as (day, month, year), returning several values from one subroutine, and as fixed keys. They also tend to be slightly more efficient because their size is known and fixed.

  6. 6

    How do you create each of an array, a record, a list and a tuple?

    Model answer

    An array is created by declaring its name, the data type of its elements and its size (and for multi-dimensional arrays the size of each dimension), which reserves the memory. A record is created by first defining a record type listing the field names and their types, then declaring a variable of that type. A list is created empty or from an initial set of values and grows as needed. A tuple is created by writing its values in order, usually in parentheses, and is complete at that moment because it cannot afterwards be altered.

Other ways to work this

The same content, four other ways.

What is inside Levo

Six ways to work the same material.

1Closed book
2Open book
3Closed book

The 3-step method, run for you

Answer cold, learn it, then prove it stuck. Levo tracks the jump between your first attempt and your last.

Processors: Structure, Performance and Types
Input, Output and Storage
Systems Software
Applications Generation

Cram a whole paper in one sitting

Sort every topic into what you know and what you do not, then drill only the weak ones.

If it matches, the search .
succeedsfailsrestarts

Blank Blitz makes facts automatic

Rapid timed gap-fills on the terms and numbers you need to recall without thinking.

A0 + 0 gives 0.
B0 + 1 gives 1.
C1 + 1 gives 0 carry 1.

Build It gets the exam shape right

Assemble a full-mark answer from its marking points, in the order an examiner wants them.

A global variable means a variable reachable from anywhere in the program.
TrueFalse

Test It catches misconceptions

Spot the fault in a statement. The traps are the mistakes students actually make.

721941163128510

Brain Breaks between sessions

Short focus games to reset attention, so the next set lands better than the last.

Every question in Data Structures, on a schedule.

Attempt a question from memory, write the correct version, then attempt it again. Levo records the jump between your first attempt and your last, and brings each question back on the day you are due to forget it.

Or £46 a year, about £3.83 a month. Cancel anytime.