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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T15:02:17+00:00 2026-06-06T15:02:17+00:00

I want to do a loop in a map. My code is like this:

  • 0

I want to do a loop in a map. My code is like this:

for (abcID,bbbID) in map(abcIdList,bbbIdList):
        buildXml(abcID,bbbID)

How should I do to make this work?

  • 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-06T15:02:18+00:00Added an answer on June 6, 2026 at 3:02 pm

    You mix up map() and zip() or dict().

    map operates on iterables and takes two arguments, a function and an iterable:

    >>> def addOne(x):
    ...     return x+1
    ... 
    >>> l = [1, 2, 3, 4]
    >>> print(map(addOne, l))
    [2, 3, 4, 5]
    

    So it applies the function passed as first argument to each element of the sequence and returns the new sequence as list (note that this behaviour is different in python3, where you get an iterable instead of a list).

    zip() instead does what you want. It takes an arbitary amount of iterables and merges together each iteration step to one tuple:

    >>> l1, l2 = [1, 2, 3, 4], [5, 6, 7, 8]
    >>> print(zip(l1, l2))
    [(1, 5), (2, 6), (3, 7), (4, 8)]
    

    Again, the result value is a list in python2 and an iterator in python3. You can iterate over that using a normal for loop like you did in your question. Of such a zipped iterator you can also create a dict (which might also be called a map, as it maps items to values):

    >>> d = dict(zip(l1, l2))
    >>> d[1]
    5
    >>> d
    {1: 5, 2: 6, 3: 7, 4: 8}
    

    However, you can not directly iterate over key-value pairs in dicts:

    >>> for key, value in d:
    ...   print(key)
    ... 
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: 'int' object is not iterable
    

    This is a rather obscure error message, as we passed a dict not an int! Where the heck does the int come from? By default, when iterating over a dict, one will iterate over the keys:

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

    But we instructed python to do unpacking on our values. So it tries to get two values out of one int, which obviously does not work. In that light, the error seems reasonable. So to iterate over key-value pairs one has to use:

    >>> for key, value in d.items():
    ...   print(key, value)
    ... 
    1 5
    2 6
    3 7
    4 8
    

    (output might differ depending on whether you imported the print_function from __future__). For large dicts and on python2, iteritems() will be faster than items(), as items() puts all key-value pairs in a list first, while iteritems() creates only a lazy iterator. On python3, items() does something similar to that already.

    I hope that clarifies the situation a bit.

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

Sidebar

Related Questions

I want to loop true certain elements, in this case all with the class
here i want to run this program and take one character pointer like char
I am using this code to convert a Set to a List : Map<String,
This days I'm refactoring code and one of the things I want to improve
Something like this. I want to brute force all characters with word of 10
I want to loop over a range of numbers from 1:n , when n
i want to loop trough all my delicious.com bookmarks and wrap a link arround
I want to loop through the checkboxgroup 'locationthemes' and build a string with all
I want to loop through the controls on a UserControl and set a property
I want to loop through a database of documents and calculate a pairwise comparison

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.