Lists

A list is R's flexible container — unlike a vector, it can hold values of different types and even nest other lists, vectors, or data frames inside it.

Learn Lists in our free R course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.

Part of the free R course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

By the end of this lesson you'll build lists with named elements, access them with $ and [[ ]], modify and extend them, and read nested structures with str().

What You'll Learn in This Lesson

1️⃣ Creating Lists

Build a list with list() , optionally naming each element with name = value . Elements can be any type — a number, a string, or a whole vector. Access named elements with the $ operator.

2️⃣ [[ ]] vs [ ], and Editing

Use [[ ]] (or $ ) to pull out the actual element; [ ] returns a smaller list . Add or change elements simply by assigning to a name.

See how scores["math"] printed as $math followed by the value — that's a list wrapper. scores[["math"]] gave the bare number.

3️⃣ Nested Lists

Lists can contain lists, which lets you model structured records — an order with a customer who has their own fields. Drill in by chaining $ or [[ ]] , and use str() to see the whole shape.

Your turn. Fill in the # TODO blank, run it, and compare with the expected output.

Write it from the outline, run it, and check it against the example output. Practising add-by-assignment and $ access cements how lists work.

📋 Quick Reference — Lists

Practice quiz

What is the key difference between a list and a vector?

  • A list is always shorter
  • A list can hold elements of different types
  • A list cannot be named
  • A list holds only numbers

Answer: A list can hold elements of different types. Unlike a vector, a list can hold mixed types and even nested structures.

Which function creates a list?

  • c()
  • vector()
  • list()
  • data()

Answer: list(). Build a list with list(), optionally naming each element.

What does single-bracket lst["a"] return for a list?

  • A smaller list
  • The bare element
  • An error
  • NULL

Answer: A smaller list. Single [ ] returns a sublist; the element stays wrapped in a list.

Which extracts the actual element (the value itself)?

  • lst("a")
  • lst{"a"}

Double brackets [[ ]] (or $) reach in and return the element itself.

How do you access a named element with the dollar operator?

  • person.name
  • person$name
  • person->name
  • person@name

Answer: person$name. The $ operator accesses a named list element, e.g. person$name.

How do you delete an element from a list?

  • Set it to NULL
  • Use drop()
  • Use remove()
  • Set it to NA

Answer: Set it to NULL. Assigning NULL, e.g. lst$a <- NULL, removes the element.

Which function reveals a list's nested structure at a glance?

  • dim()
  • summary()
  • str()
  • names()

Answer: str(). str() prints the structure of a list, including nesting.

What happens if you use $ on an atomic vector?

  • Returns the first element
  • Returns NULL silently
  • Works like on a list
  • Raises '$ operator is invalid for atomic vectors'

Answer: Raises '$ operator is invalid for atomic vectors'. $ only works on lists and data frames, not atomic vectors.

What does accessing a misspelled list name like person$naem return?

  • NULL
  • An error
  • The first element
  • NA

Answer: NULL. A misspelled name silently returns NULL; check names(person).

Why do many R functions return a list?

  • Lists print faster
  • Lists use less memory
  • Lists bundle differently-shaped parts in one object
  • Lists are required by R

Answer: Lists bundle differently-shaped parts in one object. A list neatly holds results of different lengths or types, like model output.