1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Database Python Question

  1. Jun 4, 2013 #1
    I am using python to get tuples from a db.

    I am doing

    cur.execute("Select Statement");

    I use cur.fetchall to get the tuples.

    My question is how do you iterate over the returned tuples and store the columns into an array? I want to iterate over n tuples, not all the returned results. Is storing the results into an array necessary or is the returned tuple considered an or a list?


    Thanks for the help!
     
  2. jcsd
  3. Jun 4, 2013 #2

    Dick

    User Avatar
    Science Advisor
    Homework Helper

    The only real difference betwen a tuple and a list is that tuples are not mutable (changeable). Lists are. They are both iterable. If you really want to change them you can change a tuple t into a list l with l=list(t).
     
  4. Jun 4, 2013 #3
    Ok so if the select statement returns 50 tuples that are of type (int, String) and say I only wanted 5 of them, would I have to iterate over the cur or Is there a way to just get the first 5?

    Sorry if this is a stupid question, I am just getting into python.
     
  5. Jun 4, 2013 #4

    Dick

    User Avatar
    Science Advisor
    Homework Helper

    It's not stupid. No, to get the first five elements of a tuple/list t into a tuple/list s, say s=t[:5]. Look up the way Python handles subrange indexing. It's really quite neat. You don't have to iterate to do something like that.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Database Python Question
  1. Python question (Replies: 2)

  2. Python question (Replies: 1)

  3. Newbie python question (Replies: 4)

  4. Python 3 Question (Replies: 3)

Loading...