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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T23:49:21+00:00 2026-05-20T23:49:21+00:00

I asked this question and got an excellent answer (thanks!). Part of the problem

  • 0

I asked this question and got an excellent answer (thanks!). Part of the problem to be solved involved taking a word and de-interlacing it, so that you get two words, one containing the even-indexed characters of the original word, the other containing the odd-indexed characters.

The responder used the following code to do this:

for w in words:
    even, odd = w[::2], w[1::2]

I did it this (worse) way:

for w in words:
    lst1 = []
    lst2 = []
    for c in w:
        if w.index(c) % 2 == 0:
            lst1.append(c)
        else:
            lst2.append(c)
    even = ''.join(lst1)
    odd = ''.join(lst2)

Okay, my way is worse for a number of reasons. But it seems to me as though both ways should at least produce the same pairs of words. And yet, I get different results using his implementation than mine. Why is that?

  • 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-20T23:49:22+00:00Added an answer on May 20, 2026 at 11:49 pm

    Because index(c) asks the index of the first occurrance of the letter in the word — so you are creating a single “bucket” for each letter. So if the first 'a' is odd, then all of the 'a' letters also get stuffed into the “odd” string. To fix this, you should just use enumerate() to count:

    for i, c in enumerate(word):
        # check whether ``i`` is even/odd, etc
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok, I asked this question and got excellent code example as answer. The code
I asked this question a while ago and got an answer that I thought
I asked this question before and got a great working answer. what is the
I asked this question on the SCB forum but got no answer I'm trying
I asked this question (below) a week ago and got the answer I was
I have asked this question before but I haven't got an answer, so I'm
I had asked this question before, but I got no correct answer. So, this
I asked this similar question and got a satisfactory answer. However, doing the same
I asked this question yesterday got a pretty good answer but can't figure out
I recently asked this question on here and got the answer. However I'm now

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.