# How to add variable key/value pair to list object?

I have two variables, `key` and `value`, and I want to add them as a key/value pair to a list:

``````key = "width"
value = 32

mylist = list()
mylist\$key = value
``````

The result is this:

``````mylist
# \$key
#  32
``````

But I would like this instead:

``````mylist
# \$width
#  32
``````

How can I do this?

R lists can be thought of as hashes- vectors of objects that can be accessed by name. Using this approach you can add a new entry to the list like so:

``````key <- "width"
value <- 32

mylist <- list()
mylist[[ key ]] <- value
``````

Here we use the string stored in the variable key to access a position in the list much like using the value stored in a loop variable i to access a vector through:

``````vector[ i ]
``````

The result is:

``````myList
\$width
 32
``````
• Thanks! I don't really understand this indexing operator yet. Although it is explained here: cran.r-project.org/doc/manuals/R-lang.html#Indexing, the usage you showed is not described. Is there any online doc where this is described properly? – Frank Jul 10 '09 at 3:49
• If the values are basic types (e.g. single numeric, character, etc.) you can use a regular array such as: x <- c(1, 4, 2, 5). Then assign names to each index value like: names(x) <- c('a', 'b', 'c', 'd'). You can then refer to them as x['a'], x['b'], etc. – JAponte Oct 18 '16 at 2:39

The `setNames()` built-in function makes it easy to create a hash from given key and value lists. (Thanks to Nick K for the better suggestion.)

Usage: `hh <- setNames(as.list(values), keys)`

Example:

``````players <- c("bob", "tom", "tim", "tony", "tiny", "hubert", "herbert")
rankings <- c(0.2027, 0.2187, 0.0378, 0.3334, 0.0161, 0.0555, 0.1357)
league <- setNames(as.list(rankings), players)
``````

Then accessing the values through the keys is easy:

``````league\$bob
 0.2027
league\$hubert
 0.0555
``````

List elements in R can be named. So in your case just do

`````` > mylist = list()
> mylist\$width = value
``````

When R encounters this code

``````> l\$somename=something
``````

where l is a list. It appends to a list an element something, and names it with name somename. It is then can be accessed by using

``````> l[["somename"]]
``````

or

``````> l\$somename
``````

The name can be changed with command names:

``````> names(l)[names(l)=="somename"] <- "othername"
``````

Or if you now the position of the element in the list by:

``````> names(l) <- "someothername"
``````

We can use R's list data structure to store data in the form of key-value pair.

Syntax:

`ObjectName<-list("key"= value)`

Example:

`mylist<-list("width"=32)`

also, refer example: "https://github.com/WinVector/zmPDSwR/blob/master/Statlog/GCDSteps.R"
{"ft_lang_label":"__label__en","ft_lang_prob":0.82553446,"math_prob":0.77446073,"size":269,"snap":"2020-34-2020-40","text_gpt3_token_len":93,"char_repetition_ratio":0.14716981,"word_repetition_ratio":0.0,"special_character_ratio":0.3457249,"punctuation_ratio":0.10169491,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.991177,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-08-07T21:37:24Z\",\"WARC-Record-ID\":\"<urn:uuid:f7edb730-5adc-4005-bac4-5da6263f58d7>\",\"Content-Length\":\"174730\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:a7a850aa-1c22-4851-a70e-819114e5f39b>\",\"WARC-Concurrent-To\":\"<urn:uuid:83c7392b-4b56-430f-9068-ce4f974c630a>\",\"WARC-IP-Address\":\"151.101.193.69\",\"WARC-Target-URI\":\"https://stackoverflow.com/questions/1105659/how-to-add-variable-key-value-pair-to-list-object\",\"WARC-Payload-Digest\":\"sha1:TC3J76P6TD52QVVXL6Q2ZH23MNFAT4TS\",\"WARC-Block-Digest\":\"sha1:MDXCOJF5QJC7M4UXNQHKOWVXFL2H5GJP\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-34/CC-MAIN-2020-34_segments_1596439737225.57_warc_CC-MAIN-20200807202502-20200807232502-00343.warc.gz\"}"}