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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:06:52+00:00 2026-06-01T02:06:52+00:00

I use Python/MySQLdb frequently and often format a list of categories for MySQL. The

  • 0

I use Python/MySQLdb frequently and often format a list of categories for MySQL. The below code turns a list of categories into ‘ADD COLUMN a FLOAT, ADD COLUMN b FLOAT, ADD COLUMN c FLOAT’. I want to figure out how best to simplify this piece of code, allowing for arbitrary change to generic string ‘s’ (e.g. ‘ADD COLUMN %s FLOAT’ could easily be ‘%s = %d’ or “%s = ‘%s'”). Is there some form of list comprehension which works?

categories = ['a','b','c']
select_l = []
for cat in categories:
    s = 'ADD COLUMN %s FLOAT' % (cat)
    select_l.append(s)
select_s = ", ".join(select_l)

I could create a method, but there seems to many examples where it wouldn’t handle a specific need. Thanks

  • 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-01T02:06:54+00:00Added an answer on June 1, 2026 at 2:06 am

    Using a generator expression, this can be written as a one-liner:

    select_s = ", ".join('ADD COLUMN %s FLOAT' % cat for cat in categories)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use mysql.connector (MySQLdb Python implementation?) to access MySQL. The transfer from a select
I use MySQLdb (a python library) to talk with mysql. Is there some way
When accessing a MySQL database on low level using python, I use the MySQLdb
I use MySQL with MySQLdb module in Python, in Django. I'm running in autocommit
when I use MySQLdb get this message: /var/lib/python-support/python2.6/MySQLdb/__init__.py:34: DeprecationWarning: the sets module is deprecated
I'm attempting to use Python to convert a multi-page PDF into a series of
My Pylons app uses local MySQL server via SQLAlchemy and python-MySQLdb. When the server
I'm trying to use a Class (adns-python) which is expecting a list in the
I installed python-mysqldb using sudo apt-get install python-mysqldb I have tried to use it
The code #!/usr/bin/env python import MySQLdb print Content-Type: text/html print print <html><head><title>Books</title></head> print <body>

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.