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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T20:07:57+00:00 2026-06-06T20:07:57+00:00

I have two list: set lista {5 6} set listb {8 9} and the

  • 0

I have two list:

set lista {5 6}
set listb {8 9}

and the following code is used to loop the two lists:

foreach listaelement listbelement $lista $listb {
    puts $listaelement &listbelement 
}

how can we use foreach to achieve the output is:
first element from lista, first element from listb,
first element from lista, second element from listb,
second element from lista, first element from listb,
second element from lista, second element from listb,

5 8
5 9
6 8
6 9
  • 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-06T20:07:59+00:00Added an answer on June 6, 2026 at 8:07 pm

    Just nesting the loops and using -nonewline should give the output you desire:

    foreach listelementa $lista {
       foreach listelementb $listb {
          puts -nonewline "$listelementa  $listelementb  "
       }
    }
    puts ""
    

    NB: You need to use quotes in the puts statement to stop Tcl interpreting the first argument as a channel id.

    Tcl does allow you to reference multiple lists in a foreach statement

    foreach listelementa $lista listelementb $listb {
        puts -nonewline "$listelementa  $listelementb  "
    }
    puts ""
    

    However this gives 5 8 6 9 as its output – not what you require.

    Edit: I’m pretty sure when I answered the question that the output was formatted as 1 line, if you actually want 1 line for each pair then you don’t need the -nonewline on the puts and the trailing spaces and final puts can also go.

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

Sidebar

Related Questions

i have two lists: List<comparerobj> list_c = new List<comparerobj>(); List<comparerobj> list_b = new List<comparerobj>();
I have this following test code: public static final String[] list = { apple,ball,cat,dog,egg,fan,girl,hat,igloo,jerk
I have been using the following code (with jQuery v1.4.2) to set the 'selected'
I have two List s of type int where I store point coordinates called
I have two list: list_a = [<User: x>, <User: y>] list_b = [False, True]
I have two list on my request on jsp. First one is productGroupName, and
i have two list, i wanted to update the second list while adding a
I have two list of different objects : List<Report> List<Newsletter> each having a 'created
Ho I have two list view on a frame layout. I want one overlay
Question is simple: I have two List List<String> columnsOld = DBUtils.GetColumns(db, TableName); List<String> columnsNew

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.