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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:39:34+00:00 2026-05-26T04:39:34+00:00

To operate on each element of a list, returning a modified list various languages

  • 0

To operate on each element of a list, returning a modified list various languages have explicit constructs.

In Perl there’s map:

perl -e 'my @a = (1..4); print join(q( ), map { $_ * $_ } @a)'
1 4 9 16

In Python there’re list comprehensions:

>>> a = (1,2,3,4)
>>> [el*el for el in a]
[1, 4, 9, 16]

What’s the most efficient way to do this in Tcl?
I can come up with the usual foreach loop.

set l {}
foreach i {1 2 3 4} {
    lappend l [expr $i * $i]
}
puts $l
1 4 9 16

Is this the fastest way?

Regarding mem efficiency this builds up a second list, one by one. If I don’t need the list permanently is there a more efficient way?

And, finally, is there something that’s shorter?
I couldn’t find infos here or in the http://wiki.tcl.tk

Answer:

As Donal Fellows has answered, most importantly for speed tests, things should be wrapped in a proc {} since Tcl then can optimize. For Tcl, a “map” function is discussed as a future enhancement. With this hint and further searching I found http://wiki.tcl.tk/12848

  • 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-26T04:39:35+00:00Added an answer on May 26, 2026 at 4:39 am

    The most efficient method is this:

    set idx 0
    foreach item $theList {
        lset theList $idx [expr {$item * $item}]
        incr idx
    }
    

    If the list is short (e.g., a few hundred elements) the cost of allocating a new list is minimal though, so you can use this (simpler) version instead:

    foreach item $theList {
        lappend newList [expr {$item * $item}]
    }
    

    Note that the foreach command is only fast if placed in a procedure (or lambda expression or method) and expressions are only fast if placed in {braces}. Also, don’t speculate, measure: take care to use the time command to find out how fast your code really is.

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

Sidebar

Related Questions

I have the following code which recursively operates on each element within a List
I understand how Map is easily parallelizable - each computer/CPU can just operate on
I have a task to operate on complex number. Each number consists of double
I have taken and essentially lightly modified a jQuery image slider to operate how
I have a function that accepts the element it needs to operate on as
I'm trying to do a double-loop over a std::list to operate on each pair
I have a class CommonTableModel that has several instance methods and each operate on
We have several services, each of which should ideally operate independently of each other.
The following piece of code should read each line of the file and operate
I have an application that will mainly operate in the background. I want certain

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.