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 7713307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:50:35+00:00 2026-06-01T01:50:35+00:00

Is there a way to get the list index name in my lapply() function?

  • 0

Is there a way to get the list index name in my lapply() function?

n = names(mylist)
lapply(mylist, function(list.elem) { cat("What is the name of this list element?\n" })

I asked before if it’s possible to preserve the index names in the lapply() returned list, but I still don’t know if there is an easy way to fetch each element name inside the custom function. I would like to avoid to call lapply on the names themselves, I’d rather get the name in the function parameters.

  • 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-06-01T01:50:37+00:00Added an answer on June 1, 2026 at 1:50 am

    Unfortunately, lapply only gives you the elements of the vector you pass it.
    The usual work-around is to pass it the names or indices of the vector instead of the vector itself.

    But note that you can always pass in extra arguments to the function, so the following works:

    x <- list(a=11,b=12,c=13) # Changed to list to address concerns in commments
    lapply(seq_along(x), function(y, n, i) { paste(n[[i]], y[[i]]) }, y=x, n=names(x))
    

    Here I use lapply over the indices of x, but also pass in x and the names of x. As you can see, the order of the function arguments can be anything – lapply will pass in the “element” (here the index) to the first argument not specified among the extra ones. In this case, I specify y and n, so there’s only i left…

    Which produces the following:

    [[1]]
    [1] "a 11"
    
    [[2]]
    [1] "b 12"
    
    [[3]]
    [1] "c 13"
    

    UPDATE Simpler example, same result:

    lapply(seq_along(x), function(i) paste(names(x)[[i]], x[[i]]))
    

    Here the function uses “global” variable x and extracts the names in each call.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to get an element id of a list to get
Is there a way to get a list of all open sockets ( socket
Is there a way to get the list of currently running threads in objective-C?
Is there some way to get a distinct list of file extensions on all
I was wondering if there is a way to get a list of results
Is there a way to get all versions of list item(s) using a CAML
Is there a quick way to get a flattened List<TElement> from an ILookup<TKey, TElement>
Is there a way using SWT to get a list of all processes currently
Given that: There seems to be no easy way to get a list of
is there way how to get name ov event from Lambda expression like with

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.