VOOZH about

URL: https://www.geeksforgeeks.org/r-language/how-to-add-key-value-pair-to-list-in-r/

⇱ How to add Key Value Pair to List in R ? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to add Key Value Pair to List in R ?

Last Updated : 23 Jul, 2025

A key-value pair can be explained as a set of two linked data items where a key uniquely identifies a value or a set of values in the data. Since a list can hold multiple data-types data, we can have a key-value pair stored in the list

Method 1: 

We can assign variables to each key and value. Store each key-value pair after building the list using square brackets.

Output:

21
Pulkit 

Method 2: 

Another way of doing this without using any additional variables is to specify the key and the value in the list() function while creating the list.

Example

Output:

21
Male

Method 3: Using setNames()

Another approach we can take to add a key-value pair in the list is to setNames() function and inside it, we use as.list(). Basically what we will have here is a syntax like given below, which will create a list and assign all the keys with their respective values. 

Syntax:

variable<-setNames(as.list(values), keys)

Example:

Output:

 75
Comment

Explore