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

  • Home
  • SEARCH
  • 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 1050901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:50:33+00:00 2026-05-16T16:50:33+00:00

I have a list [5, 90, 23, 12, 34, 89] etc where every two

  • 0

I have a list [5, 90, 23, 12, 34, 89] etc where every two values should be a (ranked) list in the dictionary.

So the list above would become {1: [5, 90], 2: [23, 12], 3: [34, 89]} etc. I’ve gotten close with list comprehension but haven’t cracked it. I tried:

my_list = [5, 90, 23, 12, 34, 89]
my_dict = dict((i+1, [my_list[i], my_list[i+1]]) for i in xrange(0, len(my_list)/2))

Which works for the first key, but all following values are off by one index. How would you do this?

  • 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-16T16:50:33+00:00Added an answer on May 16, 2026 at 4:50 pm

    You left a multiple of 2:

    dict( (i+1, my_list[2*i : 2*i+2]) for i in xrange(0, len(my_list)/2) )
    #                   ^
    

    BTW, you could do this instead (with Python ≥2.6 or Python ≥3.0):

    >>> it = iter(my_list)
    >>> dict(enumerate(zip(it, it), start=1))
    {1: (5, 90), 2: (23, 12), 3: (34, 89)}
    

    (of course, remember to use itertools.izip instead of zip in Python 2.x)

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

Sidebar

Related Questions

Possible Duplicate: Java - Regex problem I have list of URLs of types: http://www.example.com/pk/etc
I have a list of foods (e.g. apple, pear, banana, etc.). Suppose a user
I have two tables. Widgets, which has information about each widget (Color, size, etc);
I have two files. The first file contains a list of 6 character keys
I have a List with Checkboxes and i want to every item on the
I have a list of numbers, and I need to create every possible unique
I have List I want to sort Desc by Priority, which is int and
i have list of rows that user select and i want to delete them,
i have list of images and on mouse over there is option box shows
I have list of words. I type in a word misspelled. Can I query

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.