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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:48:25+00:00 2026-06-07T05:48:25+00:00

I have two lists that are already sorted how they need to be, and

  • 0

I have two lists that are already sorted how they need to be, and i need them put into one file, like this example:

list1 = [a, b, c, d, e]
list2 = [1, 2, 3, 4, 5]

output file should look like:

a1
b2
c3
d4
e5

im fairly new to python, so im not really all that sure how to do file writing. I read using with open(file, 'w') as f: is a better/easier way to start the writing block, but i am unsure how to merge the lists and print them as such. I could probably merge them into a third list and print that one to the file using print>>f, item but i wanted to see if there was as simpler way.

Thank you!

Late edit: looking at my lists, they wont always be the same length, but all the data needs printed regardless. So if list2 went to 7 then then the output would need to be:

a1
b2
c3
d4
e5
6
7

or vice versa, where list1 may be longer then list2.

  • 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-07T05:48:27+00:00Added an answer on June 7, 2026 at 5:48 am

    Use the zip() function to combine (ie zip) your two lists. E.g.,

    list1 = ['a', 'b', 'c', 'd', 'e']
    list2 = [1, 2, 3, 4, 5]
    
    zip(list1, list2)
    

    gives:

    [('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5)]
    

    you can then format the output to suit your needs.

    for i,j in zip(list1, list2):
        print '%s%d' %(i,j)
    

    yielding:

    a1
    b2
    c3
    d4
    e5
    

    Update:

    If your lists are unequal length, this approach using
    itertools.izip_longest() might work for you:

    import itertools
    list1 = ['a', 'b', 'c', 'd', 'e']
    list2 = [1, 2, 3]
    
    for i,j in itertools.izip_longest(list1, list2): 
        if i: sys.stdout.write('%s' %i)
        if j: sys.stdout.write('%d' %j)
        sys.stdout.write('\n')
    

    gives:

    a1
    b2
    c3
    d
    e 
    

    Note, if you were using Python 3, there is a nice way to use the print() function. I am using write() here to avoid extra blank spaces between items.

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

Sidebar

Related Questions

Specifically, I have two lists of strings that I'd like to combine into a
Suppose I have two lists that holds the list of source file names and
I have two lists that i need to combine where the second list has
I simply want to remove duplicates from two lists and combine them into one
I have two lists of objects. Each list is already sorted by a property
I have two employee lists that I want to get only unique records from
We have two lists: a=['1','2','3','4'] b=['2','3','4','5'] How to get a list with elements that
I have two pairs of drag/drop/sortable lists using JqueryUI.http://jqueryui.com/demos/sortable/ My problem is that I
I have two threads, a producer thread that places objects into a generic List
I have a function that returns two values in a list. Both values need

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.