0

I have a cursor object in python. I wish to scrape that to a list.

names = []
address = []

for x in curs:
    names.append(x["name"])
    
    if (x["country"]=='USA'):
        address.append(x["country"]+x["pincode"])
    else:
        address.append(x["country"])

this is the way I am iterating. Basically add pincode with country IFF country == USA. Else only country. Above piece works But, I wish to know same can be achieved in better/faster way. Right now it is comparing for in entry in curs the country.

2 Answers2

2

I can only give a more pythonic style, but sorry to help little on efficiency.

suppose your curs data is somehow like this:

curs = [{"name": "A", "country": "USA", "pincode": "1234"},
        {"name": "B", "country": "UK", "pincode": "5678"}, ]

then we can use:

names = [x["name"] for x in curs]
address = [x["country"] + x["pincode"] if (x["country"] == 'USA') else x["country"] for x in curs]

to get the same output:

['A', 'B']
['USA1234', 'UK']
Bowen 404
  • 102
  • 5
  • Each call on `cursor` with a for loop will fetch every item from the DB. I'm not 100% sure if this works (if you can fetch twice) and even if it did, it takes twice as much time as using the original solution. – ARK1375 Jun 15 '21 at 05:42
  • thanks, but it's another problem, in which one should add a line <`d = curs.fetchall()`> at top to fetch data only once, and use `d` as the iterable in the following. – Bowen 404 Jun 15 '21 at 07:01
0

From what I read in MySQL cursor docs and this post on Stack Overflow, the code snippet below should work fine.

def decider(val):
    if( val['country'] == 'USA'):
        return val["country"]+ val["pincode"]

    else return
        val["country"]

data = curs.fetchall()
names , adress = map(lambda x: x["names"] , data ) , map(decider , data )

However, since cursor.__iter__() will fetch items one by one from the DB, performance vise I don't think that there is a difference between using a for loop and a map.
If you want to improve your code performance, you have to use async programming. It goes something like this:
Fetch the first row from the DB. Begin fetching the second row. While waiting, perform any action you want on the first row until the second row is fetched.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
ARK1375
  • 790
  • 3
  • 19