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 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

Is there a rake task for backing up the data in your database? I
Task: implement paging of database records suitable for different RDBMS. Method should work for
The task is to take a list of tables which is changeable. Write a
The task is simple, and the answers might be many. But here goes: On
Simple task like make AJAX request , pass one parameter and return result, can
I've got the task of displaying a web form to represent the properties in
Right now my ant task looks like. <javadoc sourcepath=${source} destdir=${doc}> <link href=http://java.sun.com/j2se/1.5.0/docs/api/ /> </javadoc>
A common task in programs I've been working on lately is modifying a text
I was given a task to display when a record in the database was
When I use the task, the property is only set to TRUE if the

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.