Menu
Coddy logo textTech

Named Lists

Part of the Fundamentals section of Coddy's R journey — lesson 74 of 78.

Just like named vectors, lists can have names assigned to their elements. This makes your code more readable and allows you to access elements by meaningful labels instead of numeric positions.

You can assign names when creating the list:

person <- list(name = "Alice", age = 25, active = TRUE)
print(person)

Output:

$name
[1] "Alice"

$age
[1] 25

$active
[1] TRUE

Notice how the output now shows $name instead of [[1]]. To access named elements, use the $ operator:

person <- list(name = "Alice", age = 25, active = TRUE)
print(person$age)

Output:

[1] 25

You can also use double brackets with the name as a string, which is useful when the name is stored in a variable:

person <- list(name = "Alice", age = 25)
print(person[["name"]])

Output:

[1] "Alice"

Modifying named elements works the same way. Use $ to change existing values or add new ones:

person <- list(name = "Alice", age = 25)
person$age <- 26
person$city <- "Paris"
print(person)

Output:

$name
[1] "Alice"

$age
[1] 26

$city
[1] "Paris"
challenge icon

Challenge

Easy

You will receive three lines of input:

  1. A book title (e.g., The Great Gatsby)
  2. A book author (e.g., F. Scott Fitzgerald)
  3. The year published (e.g., 1925)

Perform the following operations:

  1. Create a named list called book with three elements: title, author, and year (convert year to numeric)
  2. Print the book's title using the $ operator
  3. Print the book's year using double brackets with the name as a string
  4. Update the year to be 1 year later using the $ operator
  5. Add a new element called available with the value TRUE
  6. Print the complete updated list

For example, if the inputs are:

1984
George Orwell
1949

The output should be:

[1] "1984"
[1] 1949
$title
[1] "1984"

$author
[1] "George Orwell"

$year
[1] 1950

$available
[1] TRUE

Explanation:

  • book$title prints the title value
  • book[["year"]] prints the original year
  • The year is updated from 1949 to 1950
  • A new available element is added with value TRUE

Cheat sheet

Lists can have names assigned to their elements for better readability and access by meaningful labels.

Create a named list by assigning names during creation:

person <- list(name = "Alice", age = 25, active = TRUE)

Access named elements using the $ operator:

person$age  # Returns 25

Access named elements using double brackets with the name as a string:

person[["name"]]  # Returns "Alice"

Modify existing named elements:

person$age <- 26

Add new named elements:

person$city <- "Paris"

Try it yourself

# Read input
con <- file("stdin", "r")
title <- suppressWarnings(readLines(con, n = 1))
author <- suppressWarnings(readLines(con, n = 1))
year <- suppressWarnings(readLines(con, n = 1))
close(con)

# TODO: Write your code below
# 1. Create a named list called 'book' with elements: title, author, and year (convert year to numeric)
# 2. Print the book's title using the $ operator
# 3. Print the book's year using double brackets with the name as a string
# 4. Update the year to be 1 year later using the $ operator
# 5. Add a new element called 'available' with value TRUE
# 6. Print the complete updated list
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals