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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:50:06+00:00 2026-05-27T19:50:06+00:00

In Python, I have two lists that either have equal number of elements (e.g.

  • 0

In Python, I have two lists that either have equal number of elements (e.g. 8 and 8) or one less than the other (e.g. 7 and 8; 3 and 4):

list1 = ['A', 'B', 'C', 'D']
list2 = ['E', 'F', 'G', 'H']

or

list3 = ['A', 'B', 'C']
list4 = ['D', 'E', 'F', 'G']

I’m trying to figure out the best way to build an algorithm that will switch the last half of the first list with the first half of the last list, resulting in this, when both lists have an even number of elements:

switched_list1 = ['A', 'B', 'E', 'F']
switched_list2 = ['C', 'D', 'G', 'H']

…and this when the one of the lists has an odd number:

switched_list3 = ['A', 'D', 'E']
switched_list4 = ['B', 'C', 'F', 'G']

What’s the most efficient way to build an algorithm that can switch list elements like 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-27T19:50:07+00:00Added an answer on May 27, 2026 at 7:50 pm
    >>> def StrangeSwitch(list1,list2):
        return (list1[:len(list1)/2]+list2[:len(list2)/2],list1[len(list1)/2:]+list2[len(list2)/2:])
    
    >>> list1 = ['A', 'B', 'C', 'D']
    >>> list2 = ['E', 'F', 'G', 'H']
    >>> (list1,list2)=StrangeSwitch(list1,list2)
    >>> list1
    ['A', 'B', 'E', 'F']
    >>> list2
    ['C', 'D', 'G', 'H']
    >>> list3 = ['A', 'B', 'C']
    >>> list4 = ['D', 'E', 'F', 'G']
    >>> (list3,list4)=StrangeSwitch(list3,list4)
    >>> list3
    ['A', 'B', 'C']
    >>> list4
    ['B', 'C', 'F', 'G']
    >>> 
    

    Reading the Comments by OP I would take the priviledge of proposing another approach

    >>> def StrangeSwitchFast(list1,list2):
        #return (list1[:len(list1)/2]+list2[:len(list2)/2],list1[len(list1)/2:]+list2[len(list2)/2:])
        return (list(itertools.chain(itertools.islice(list1,0,len(list1)/2),itertools.islice(list2,0,len(list2)/2))),
            list(itertools.chain(itertools.islice(list1,len(list1)/2,None),itertools.islice(list2,len(list2)/2,None))))
    

    The above doesn’t create any temporary list and if OP desires to use it as an iterator rather than a list for the downstream processing, then the list can be safely dropped from the function and can be left to return as a tuple of iterators.

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

Sidebar

Related Questions

I have two lists of dictionaries list1 = [ {..}, {..}, ..] list2 =
I have two lists created in python like so: list1 = [2, 3, 3,
I have two lists old and new , with the same number of elements.
I have two String.printable mysteries in the one question. First, in Python 2.6: >>>
I have two lists in python and I want to know if they intersect
Working in python 2.7. I have two lists (simplified to make explanations clearer). T
Working in python 2.7. I have two lists (simplified to make explanation more clear):
i want to find the intersection of two lists in python. i have something
I'm using R, and I'm a beginner. I have two large lists (30K elements
I have two lists that I am trying to compare with < or >

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.