Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL SQLITE MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST DART PLAYWRIGHT TOOLS MATH SHEETS POWER BI

R Named Lists


Share

Named Lists

In the Lists chapter, you got the items by their position: thislist[1], thislist[2], and so on.

That works for short lists, but in a longer list it is hard to remember which position holds which value. Instead, you can give each item a name, like the fields on a contact card:

Example

person <- list(name = "John", age = 40, married = TRUE)

person
Try it Yourself »

R prints the name of each item with a $ sign in front of it.


Get an Item by Name

Use the $ sign, or double brackets [[ ]] with the name in quotes, to get the value of an item:

Example

person <- list(name = "John", age = 40, married = TRUE)

person$name
person[["age"]]
Try it Yourself »

Note: This is the same $ sign that you use to get a column from a data frame. A data frame is a list of columns, where each column has a name.


[ ] or [[ ]]?

Single brackets [ ] return a smaller list. Double brackets [[ ]] return the value itself:

Example

person <- list(name = "John", age = 40, married = TRUE)

class(person["name"])
class(person[["name"]])
Try it Yourself »

Think of the list as a train with wagons: [ ] gives you a wagon, and [[ ]] gives you what is inside the wagon.



Change, Add and Remove Items

Use the $ sign to change an item, or to add a new one. To remove an item, set it to NULL:

Example

person <- list(name = "John", age = 40, married = TRUE)

person$age <- 41
person$job <- "Teacher"
person$married <- NULL

person
Try it Yourself »

Get the Names

The names() function returns the names of all the items:

Example

person <- list(name = "John", age = 40, married = TRUE)

names(person)
Try it Yourself »

You can use it to loop through the list, and print both the name and the value of each item:

Example

person <- list(name = "John", age = 40, married = TRUE)

for (key in names(person)) {
  print(paste(key, ":", person[[key]]))
}
Try it Yourself »


Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by W3Schools. All Rights Reserved. W3Schools is Powered by W3.CSS.