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

  • Context: Python 
  • Thread starter Thread starter Punkyc7
  • Start date Start date
  • Tags Tags
    Database Python
Click For Summary

Discussion Overview

The discussion revolves around retrieving specific elements from tuples or lists in Python, particularly in the context of fetching data from a database. Participants explore methods to access a subset of tuples without iterating through all results, addressing both conceptual and practical aspects of Python's data structures.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Homework-related

Main Points Raised

  • One participant describes using a database cursor to fetch tuples and questions how to store specific columns into an array without iterating over all results.
  • Another participant explains the difference between tuples and lists, noting that tuples are immutable while lists are mutable, and suggests converting a tuple to a list if changes are needed.
  • A participant asks if it is necessary to iterate over the cursor to obtain a specific number of tuples, specifically the first five, and expresses uncertainty about the process.
  • In response, another participant reassures that it is not a stupid question and provides a method to access the first five elements using Python's subrange indexing without iteration.

Areas of Agreement / Disagreement

Participants generally agree on the differences between tuples and lists, as well as the ability to access elements using indexing. However, there is no consensus on the necessity of iteration to retrieve a specific number of tuples, as some participants suggest alternatives while others seek clarification.

Contextual Notes

The discussion includes assumptions about the structure of the returned tuples and the specific requirements for data manipulation, which may not be fully articulated. There are also unresolved questions regarding the implications of using arrays versus lists in this context.

Who May Find This Useful

Individuals learning Python, particularly those interested in database interactions and data manipulation techniques, may find this discussion relevant.

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

Similar threads

  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 16 ·
Replies
16
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 15 ·
Replies
15
Views
5K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 7 ·
Replies
7
Views
5K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 29 ·
Replies
29
Views
4K