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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:36:58+00:00 2026-06-18T14:36:58+00:00

I am fetching data for too many users from DB , the array in

  • 0

I am fetching data for too many users from DB , the array in which all data is collected having two rows for each user , only last column value is different rest of the values are same like

r1 --> a , b , c , d , 1 (user1)
r2 -- > a , b , c , d , 2 (user1)
r3 --> z , x , v , n , 3 (user2)
r4 -- > z , x , v , n , 4 (user2)

Now I want to create an array which include only one row corresponding to each user but consist of every value like

r1 --> a , b , c , d , 1 , 2 (user1)
r2 -- >z , x , v , n , 3 , 4 (user2)

I have tried for loop , but the resulted array consist of only last record , I wanted an array which consist of all the records for all users.

e.g.

cursor.execute("some query ")
    numrows = int(cursor.rowcount) 

let this num rows returns value 4.

then

for I in range(numrows):

pass

will loop 4 times , but actually it consist the values for two users.

but when I returns the resulted array it consist the value of last user only.

So please suggest me some way so that when I do this for I in range(numrows): the array it returns consist of all the records not only last user record

Edited:

I have also tried while loop as

arr=[]
while arr in row:
   arr= row[0][1]

but when i return this arr , it is empty but if i return row[0][1] then it shows the value of that column.

Thanks in advance.

  • 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-18T14:36:59+00:00Added an answer on June 18, 2026 at 2:36 pm

    Assuming that your data is sorted by first 4 entries (which you can do in your SQL), you can transform your data using the following code:

    from itertools import groupby
    from operator import itemgetter
    
    rows=[
        ('a' , 'b' , 'c' , 'd' , 1),
        ('a' , 'b' , 'c' , 'd' , 2),
        ('z' , 'x' , 'v' , 'n' , 3),
        ('z' , 'x' , 'v' , 'n' , 4)]
    
    
    
    out = []
    for key,group in groupby(rows, itemgetter(0,1,2,3)):
        new_row = list(key)
        for v in group:
            new_row.append(v[4])
        out.append(new_row)
    
    print out
    

    which prints

    [['a', 'b', 'c', 'd', 1, 2], ['z', 'x', 'v', 'n', 3, 4]]
    

    Here is another idea for you: use your first 4 entries as keys in a dictionary.

    from collections import defaultdict
    
    rows=[
        ('a' , 'b' , 'c' , 'd' , 1),
        ('a' , 'b' , 'c' , 'd' , 2),
        ('z' , 'x' , 'v' , 'n' , 3),
        ('z' , 'x' , 'v' , 'n' , 4)]
    
    
    
    d = defaultdict(list)
    for r in rows:
        d[tuple(r[0:4])].append(r[4])
    
    print d[('a' , 'b' , 'c' , 'd')]
    print d[('z' , 'x' , 'v' , 'n')]
    

    prints

    [1, 2]
    [3, 4]
    

    With this approach it does not matter if your rows are sorted by first 4 columns

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

Sidebar

Related Questions

I'm fetching data from all 3 tables at once to avoid network latency. Fetching
This is my php script for fetching data from a db. <?php mysql_connect(host,user,pass); mysql_select_db(db);
Now a lot of scripts to facebook-style fetching data from url, but all of
I am developing an iPhone application in which I am fetching data from a
I am fetching data from multiple table having 600 thousand of records. But is
i am having problems while fetching data from kml file. geoXml.parse(http://localhost/maps/phpmysqlajax_genkml.kml); error in console
I am having 2 issues with fetching data from doctrine: Select records from start
I am fetching data from some tables & storing it in a variable like
I am fetching data from mysql to a page, i am not sure if
I have an App using UITableViews and fetching data from a server. I am

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.