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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:19:08+00:00 2026-05-17T23:19:08+00:00

Task at hand is to move data as shown in table 1 to that

  • 0

Task at hand is to move data as shown in table 1 to that of table 2.

Table (1)

ID  Val
--  ---
1   a
1   b
1   c
2   k
3   l
3   m 
3   n

Val columns depend on the number of unique values for each ID. in this case it is 3 but it can be 20 in real world!

Table (2)

ID  Val1 Val2 Val3  
--  --   --   --
1   a    b    c
2   k
3   l    m    n

How am I tackling it for smaller values of Val columns (3 in this case) :

I create a temp table.

create table test(ID int not null, b int auto_increment not null,primary key(ID,b), Val varchar(255));

I then insert data in to test.

I get the following (I have to create the Val columns manually):

ID  Val  b
--  ---  --
1   a    1
1   b    2
1   c    3
2   k    1
3   l    1
3   m    2
3   n    3

I know that this is a tedious process with lot of manual work. This was before I fell in love with Python! An efficient solution in Python for this problem is really appreciated!

This is what I have so far

import MySQLdb
import itertools
import dbstring

cursor = db.cursor()

cursor.execute("select ID, val from mytable")
mydata = cursor.fetchall()

IDlist = []
vallist = []
finallist = []

for record in mydata:
    IDlist.append(record[1])
    vallist.append(record[2])

zipped = zip(IDlist,vallist)
zipped.sort(key=lambda x:x[0])

for i, j in itertools.groupby(zipped, key=lambda x:x[0]):
    finallist = [k[1] for k in j]
finallist.insert(0, i)
finallist += [None] * (4 - len(finallist))  ### Making it a uniform size list
    myvalues.append(finallist)

cursor.executemany("INSERT INTO temptable VALUES (%s, %s, %s, %s)", myvalues)


db.close()
  • 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-17T23:19:08+00:00Added an answer on May 17, 2026 at 11:19 pm

    the pytonic way to do this is to use itertools.groupby

    import itertools
    
    a = [(1, 'a'), (1, 'b'), (2, 'c')]
    
    # groupby need sorted value so sorted in case
    a.sort(key=lambda x:x[0])
    
    for i, j in itertools.groupby(a, key=lambda x:x[0]):
        print i, [k[1] for k in j]
    

    return

    1 ['a', 'b']
    2 ['c']
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

a task at my hand requires me to move an ASP application from one
I have a task in hand that requires me to send a form to
I am new this sharepoint development and i have task in hand to do
Is there a rake task for backing up the data in your database? I
Task at hand — I have three versions of some code, developed by different
I have a task at hand of creating some kind of logic for a
I have a very simple task at hand. If the last time a record
I am facing with a huge task at hand, start re-factoring our biggest asp.net
Let's say we have a very processor-intensive task at hand which could be effectively
I am trying to complete a seemingly simple task that has turned into a

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.