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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:10:18+00:00 2026-05-24T03:10:18+00:00

Just like I’m allowed to do this: (EDIT – I’m NOT allowed to do

  • 0

Just like I’m allowed to do this:

(EDIT – I’m NOT allowed to do this either, sorry messed up, but anyway I guess my question is more on the lines of how to go about doing parallel assignments in a loop)

for (x,i) in ([0,1,2],[3,4,5]):
  # do something

why am I not allowed to do this in a for each loop (different list length)

 for (x,i) in ([4,6,5,7,8,9],[1,2,3]):
   # do something

I know you get a “ValueError: too many values to unpack”, but why doesn’t the loop stop when i is done with its iteration?

A real use of this would be for something like this that I was trying to do

for (keys,i) in (sorted(dic.keys(),key=custom_sort), range(10)):
  print dic[keys]

where I’m sorting the dictionary as well as printing only the first 10 top results (assume the dictionary has hundreds of keys) – I only wanted the top 10 results.

Since this kind of syntax is not allowed anyway what could be the next best thing to do?

thanks

  • 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-24T03:10:19+00:00Added an answer on May 24, 2026 at 3:10 am

    For this kind of stuff, itertools is your friend.

    In your case, itertools.islice() will do the trick:

    import itertools
    for key in itertools.islice(sorted(dict.keys(), key=custom_sort), 10):
        # Only the first 10 keys are processed.
    

    Alternatively, if you need the index as well, use zip or itertools.izip:

    for i, key in zip(range(10), sorted(dict.keys(), key=custom_sort)):
        # i will be 0, 1, ..., 9
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just like in the question, not sure what the difference between '' and N''
Just like the question asks. Does 5.4 address this?
Just like Carl's question over here I would like to ask you (because I
Just like stackoverflow, there is a many-to-many relationship between Question and Tag. After running
Just like in this topic , I have a performance issue in dev mode
Just like the title, how would a multiget from memcache look like? Is this
Just like in SO, where one is answering a question, if somebody has answered
Just like the question says: Is there a way to invoke case- insensitive substr_count()?
just like my question, How can i convert decimal into octal in objective c?
Just like hold on in Matlab. I met this problem when i want to

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.