Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6984713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:35:07+00:00 2026-05-27T18:35:07+00:00

Suppose we would like to store pairs as elements in a list, e.g., >

  • 0

Suppose we would like to store pairs as elements in a list, e.g.,

> x <- list()
> x[[1]] <- list(1,2)
> x[[2]] <- list(3,4)
> x
[[1]]
[[1]][[1]]
[1] 1

[[1]][[2]]
[1] 2


[[2]]
[[2]][[1]]
[1] 3

[[2]][[2]]
[1] 4

If one wants to create the same thing with concatenation, without explicit reference to an index of the outer list, a natural approach (see EDIT below for motivation for this approach) is

> c(list(list(1,2)),list(3,4))
[[1]]
[[1]][[1]]
[1] 1

[[1]][[2]]
[1] 2


[[2]]
[1] 3

[[3]]
[1] 4

Is there a way to avoid the “flattening” of the second argument to c() and thereby use concatenation to produce x?

EDIT: I should have made my intended application clear.

In the end, I would like to add pairs to a list whenever I find one as I go through a loop. Currently I maintain an index for the main list to add new pairs to the end of it when one is found, however using c is perhaps more natural. So using an explicit index the loop may look like this

index <- 1
l <- list()
for (i in 1:10) {
    for (j in (i+1):10) {
        if ( (i+j)%%2 == 0 ) {
            l[[index]] <- list(i,j)
            index <- index + 1
        }
    }
}

And the failed attempt to use c looks like this

l <- list()
for (i in 1:10) {
    for (j in (i+1):10) {
        if ( (i+j)%%2 == 0 )
            l <- c(l,list(i,j))
    }
}

ANSWER: as noted by Tommy below, an extra list() is needed, so the following works:

l <- list()
for (i in 1:10) {
    for (j in (i+1):10) {
        if ( (i+j)%%2 == 0 )
            l <- c(l,list(list(i,j)))
    }
}

Kind of obvious thing I should have figured out. Tommy also notes that this might not be a very good way of doing things.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T18:35:07+00:00Added an answer on May 27, 2026 at 6:35 pm

    I guess the simple answer is “no, you need the extra list() wrapper”.

    …but in general, it is better (faster/less memory) to add values using indexing to a pre-allocated list than using c()

    x <- NULL
    for(i in 1:2) x <- c(x, list(list(i,i+1)))
    x
    
    x <- vector('list', 2)
    for(i in seq_along(x)) x[[i]] <- list(i,i+1)
    x
    

    And if lapply can be used, that is usually even more efficient:

    lapply(1:2, function(i) list(i, i+1))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to ask how licensing works on multiple instances? Suppose that I
Let's suppose I'm using the Northwind database and I would like to run a
Suppose I have this Windows wchar_t string: L\x4f60\x597d and L\x00e4\x00a0\x597d and would like to
Suppose I want to implement an interpreter for a functional language. I would like
Suppose that you have to set 12 cookies, would it be better to store
Suppose you store instances of a class in a relational table. How would you
Suppose a tree structure is implemented in SQL like this: CREATE TABLE nodes (
I have several times ran into the same problem, and I would like to
Supposed I have the following string: string str = <tag>text</tag>; And I would like
Would it suppose any difference regarding overhead to write an import loading all the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.