Menu
Coddy logo textTech

Accessing List Elements

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

Lists use a different syntax for accessing elements compared to vectors. The key difference is between single brackets [ ] and double brackets [[ ]].

Double brackets extract the actual element from the list:

my_list <- list(42, "hello", TRUE)
print(my_list[[1]])

Output:

[1] 42

Single brackets return a sublist containing the element, not the element itself:

my_list <- list(42, "hello", TRUE)
print(my_list[1])

Output:

[[1]]
[1] 42

This distinction matters when you want to work with the actual value. If a list element is a vector, double brackets let you access it directly and then use vector indexing:

my_list <- list(c(10, 20, 30), "text")
print(my_list[[1]][2])

Output:

[1] 20

Here, my_list[[1]] retrieves the vector, and [2] accesses its second element. Remember: use [[ ]] when you need the actual content, and [ ] when you want a smaller list.

challenge icon

Challenge

Easy

You will receive two lines of input:

  1. Three comma-separated values of different types (e.g., 100,hello,TRUE)
  2. A comma-separated list of numbers representing a vector to be stored as the fourth element (e.g., 5,10,15,20)

Perform the following operations:

  1. Create a list with four elements: the numeric value, the text, the logical value, and the vector of numbers
  2. Use double brackets to extract and print the second element (the text value)
  3. Use single brackets to extract the third element and print the result (notice the difference in output format)
  4. Access the vector stored in the fourth element using double brackets, then extract and print its third value using vector indexing
  5. Print the sum of all numbers in the fourth element (access the vector with double brackets and use sum())

For example, if the inputs are:

42,world,FALSE
8,16,24,32,40

The output should be:

[1] "world"
[[1]]
[1] FALSE

[1] 24
[1] 120

Explanation:

  • my_list[[2]] extracts the actual text value: "world"
  • my_list[3] returns a sublist containing FALSE (note the [[1]] in output)
  • my_list[[4]][3] accesses the vector, then gets its third element: 24
  • sum(my_list[[4]]) calculates 8+16+24+32+40 = 120

Cheat sheet

Lists use different bracket syntax for accessing elements:

Double brackets [[ ]] extract the actual element:

my_list <- list(42, "hello", TRUE)
print(my_list[[1]])  # Output: [1] 42

Single brackets [ ] return a sublist containing the element:

my_list <- list(42, "hello", TRUE)
print(my_list[1])  # Output: [[1]] [1] 42

Accessing nested elements: Use double brackets to extract a vector from a list, then use vector indexing:

my_list <- list(c(10, 20, 30), "text")
print(my_list[[1]][2])  # Output: [1] 20

Use [[ ]] when you need the actual content, and [ ] when you want a smaller list.

Try it yourself

# Read input
con <- file("stdin", "r")
input1 <- suppressWarnings(readLines(con, n = 1))  # First line: three comma-separated values
input2 <- suppressWarnings(readLines(con, n = 1))  # Second line: comma-separated numbers for vector
close(con)

# Parse the first input line into separate values
values <- strsplit(input1, ",")[[1]]
numeric_val <- as.numeric(values[1])
text_val <- values[2]
logical_val <- as.logical(values[3])

# Parse the second input line into a numeric vector
vector_val <- as.numeric(strsplit(input2, ",")[[1]])

# TODO: Write your code below
# 1. Create a list with four elements: numeric_val, text_val, logical_val, and vector_val
# 2. Use double brackets [[ ]] to extract and print the second element
# 3. Use single brackets [ ] to extract the third element and print it
# 4. Access the fourth element with double brackets, then extract its third value
# 5. Print the sum of all numbers in the fourth element
quiz iconTest yourself

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

All lessons in Fundamentals