Python How can I get specific elements from a tuple/list in Python without iterating?

  • Thread starter Thread starter Punkyc7
  • Start date Start date
  • Tags Tags
    Database Python
AI Thread Summary
To retrieve specific elements from tuples in Python without iteration, you can use slicing. When executing a SQL query, you can fetch results as tuples using `cur.fetchall()`. If you want only the first five tuples from the result, you can use slicing like `s = t[:5]`, which avoids the need for iteration. Tuples are immutable, while lists are mutable, but both are iterable. This method allows for efficient data handling without unnecessary loops.
Punkyc7
Messages
415
Reaction score
0
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!
 
Technology news on Phys.org
Punkyc7 said:
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!

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).
 
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.
 
Punkyc7 said:
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.

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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Back
Top