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

  • Home
  • SEARCH
  • 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 9270805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:27:40+00:00 2026-06-18T15:27:40+00:00

im beginner with python.I want to convert sql results to a list.Here’s my code:

  • 0

im beginner with python.I want to convert sql results to a list.Here’s my code:

cursor = connnect_db()

query = "SELECT * FROM `tbl`"

cursor.execute(query)

options = list()

for i,row in enumerate(cursor.fetchall()):
   options.append(row[i])

There is 6 column in my table but this code doesn’t create 6 element-list.Where am i doing wrong?

  • 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-18T15:27:41+00:00Added an answer on June 18, 2026 at 3:27 pm

    If you have an iterable in Python, to make a list, one can simply call the list() built-in:

    list(cursor.fetchall())
    

    Note that an iterable is often just as useful as a list, and potentially more efficient as it can be lazy.

    Your original code fails as it doesn’t make too much sense. You loop over the rows and enumerate them, so you get (0, first_row), (1, second_row), etc… – this means you are building up a list of the nth item of each nth row, which isn’t what you wanted at all.

    This code shows some problems – firstly, list() without any arguments is generally better replaced with an empty list literal ([]), as it’s easier to read.

    Next, you are trying to loop by index, this is a bad idea in Python. Loop over values, themselves, not indices you then use to get values.

    Also note that when you do need to build a list of values like this, a list comprehension is the best way to do it, rather than creating a list, then appending to it.

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

Sidebar

Related Questions

Python beginner here, I have a list of lists and want to refer to
Python beginner here. I am using the matplotlib library to make graphs from tab
Python beginner running 2.7 I want to have a list which is constantly summed
I am still a python beginner. As a practice project I want to code
beginner to python here. I have 2 nested lists that I want to merge:
I am a complete beginner to Python so dont understand the lingo. I want
I'm a beginner, with programming and Python. What I want to do is read
Python beginner here. I have a text file that is sorted into columns: fields
I am just a beginner in python and I want to know is it
I'm a beginner in python. All i want to do is create a simple

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.