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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T14:04:12+00:00 2026-06-07T14:04:12+00:00

I would like to join two lines in file, based on whether or not

  • 0

I would like to join two lines in file, based on whether or not they start with the same element.
I could turn the first element of each line into a list, and use the elements in this list to search each line, but that hardly seems the most efficient way?

I have the following file

1,AF534061.1,T,A  
1,K02718.1,T,A  
16,AF534061.1,G,-  
16,K02718.1,G,-  
17,AF534061.1,T,-  
17,K02718.1,T,-  
18,AF534061.1,A,-  
18,K02718.1,A,-  
19,AF534061.1,T,-  
19,K02718.1,T,-  
20,AF534061.1,A,-  
20,K02718.1,A,-  
21,AF534061.1,A,-   
21,K02718.1,A,-  
24,AF534061.1,C,T   

I would like to join lines if the first item is shared between the lines. So I would like to get the following output

1,AF534061.1,T,A,1,K02718.1,T,A
16,AF534061.1,G,-,16,K02718.1,G,-
17,AF534061.1,T,-,17,K02718.1,T,-
18,AF534061.1,A,-,18,K02718.1,A,-
19,AF534061.1,T,-,19,K02718.1,T,-
20,AF534061.1,A,-,20,K02718.1,A,-
21,AF534061.1,A,-,21,K02718.1,A,-
24,AF534061.1,C,T

In this example, it looks like I might just be able to join every-other line, but I want (need) to make the code more general!

I do not think this is hard, but I cannot seem to figure it out!
thanks for the help

  • 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-07T14:04:13+00:00Added an answer on June 7, 2026 at 2:04 pm

    The Python standard library is full of tools. For this job, use itertools.groupby.

    import itertools
    
    lines = '''1,AF534061.1,T,A
    1,K02718.1,T,A
    16,AF534061.1,G,-
    16,K02718.1,G,-
    17,AF534061.1,T,-
    17,K02718.1,T,-
    18,AF534061.1,A,-
    18,K02718.1,A,-
    19,AF534061.1,T,-
    19,K02718.1,T,-
    20,AF534061.1,A,-
    20,K02718.1,A,-
    21,AF534061.1,A,-
    21,K02718.1,A,-
    24,AF534061.1,C,T'''.split('\n')
    
    for key, group in itertools.groupby(lines, lambda line: line.partition(',')[0]):
        print ','.join(group)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to know whether I could perform a full outer join between
I would like to join and process the two lines coming out of the
I would like to join two xml files as illustrated below. However the question
I have two tables user and log. I would like to join the user
I have two variables which are comma separated lists. I would like to join
How would I join two lines using awk or sed? I have data that
I would like to be able to display two lines of text per row
I would like to join the same table 4 times to get columns way
I would like to create an optional ManyToOne association between two entities based on
I have a materialized view of a complex join, which I would like 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.