levo

Data Structures

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

  • 2 things to know
  • Component 01
Start revising · £5/month

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.

Everything in your subscription

Runs, plus a mode for every other kind of session.

1Flash
2Imposter
3Fade
4Brain dump

Learn every subtopic in runs of four questions

Facts flash and fade, a wrong word hides in some of them, and a brain dump decides which questions are locked in.

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

Cram the night before, weakest topic first

A quick scan finds your weakest topics. Recall each fact you missed three times in a row.

If it matches, the search .
succeedsfailsrestarts

Blank Blitz consolidates the facts

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

✓What it iscovered
~Why it happenspartly there
How long it lastsnot there yet

Explain It tests your understanding

Explain a concept in your own words and see which of its points you covered.

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

Test It catches misconceptions

Spot the fault in a statement. The faults are the mistakes students make most often.

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.

Learn it in runs of four questions, each ending on a brain dump against the clock. Every question you lock in comes back the next day, then at longer gaps, each time just before you would forget it.

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