convert list into a comma separated string

1,794 views
Skip to first unread message

ashy

unread,
Oct 4, 2010, 7:41:36 AM10/4/10
to Django users
Hi All,

I am writing a django function in which I have a following list:
li = ['a','b','c']
I want to create a string from the list which should look like
str = 'a,b,c'

I plan to pass this string to not in () function of my sql query.

Any Ideas?

thanks
ashy



Scott Gould

unread,
Oct 4, 2010, 8:40:06 AM10/4/10
to Django users
Python has you covered:

my_string = ",".join(my_list)

HTH,
Scott

Shawn Milochik

unread,
Oct 4, 2010, 8:39:49 AM10/4/10
to django...@googlegroups.com
','.join(your_list)

Carlton Gibson

unread,
Oct 4, 2010, 8:40:05 AM10/4/10
to django...@googlegroups.com

On 4 Oct 2010, at 12:41, ashy wrote:

> I am writing a django function in which I have a following list:
> li = ['a','b','c']
> I want to create a string from the list which should look like
> str = 'a,b,c'

','.join(li)

Regards,
Carlton

ashy

unread,
Oct 4, 2010, 8:49:21 AM10/4/10
to Django users
Hi All,

my code is as below:
li = ['2l','1l']
str = ",".join(li)

but print str does not work for me :(

Steve Holden

unread,
Oct 4, 2010, 9:10:24 AM10/4/10
to django...@googlegroups.com
"Does not work" is hardly a sufficient description of your problem, I
suspect. If I tell you my car "does not work", would you be able to tell
me how to fix it?

How are you running Django? If you are running it using

python manage.py runserver

for testing then print statements *should* cause output to appear in the
standard output stream. If you are running under Apache, however, that
is not the case.

Also note that "str" is not a happy choice of name, since it is also the
name of one of Python's basic types and you may wish to use it as such.
Having a variable called "str" in your code stops you from doing so.

A little more information will allow us to help you better.

regards
Steve
--
DjangoCon US 2010 September 7-9 http://djangocon.us/

ashwin morey

unread,
Oct 4, 2010, 9:27:00 AM10/4/10
to django...@googlegroups.com
Hi,

I am using django development server and not apache. But the print str is not working.
I tried changing str to s, but no luck.

thanks


Shawn Milochik

unread,
Oct 4, 2010, 9:57:23 AM10/4/10
to django...@googlegroups.com
I think you would benefit greatly from doing a Python tutorial. 


Remember that Django is just Python. If you're asking a Python question this basic on the Django list (which is perfectly fine), the most helpful thing we can do for you at this point is guide you to learning enough about Python so that you know when you have a Django question and when it's a basic Python question.

In addition, you may want to join the Python mailing list:

Shawn

Aaron Sterling

unread,
Oct 4, 2010, 8:40:40 AM10/4/10
to django...@googlegroups.com
On Mon, Oct 4, 2010 at 1:41 AM, ashy <ashwi...@gmail.com> wrote:
Hi All,

I am writing a django function in which I have a following list:
li = ['a','b','c']
I want to create a string from the list which should look like
str = 'a,b,c'


string_ = "".join(li)

str is a built in class. You shouldn't shadow it. it's better to shadow string which is just a module and best to use string_ or something else.


 
I plan to pass this string to not in () function of my sql query.


Are you getting this list from user input? don't do that. I'm new to django so I can't recommend a better way but don't trust user input
 

cajbe

unread,
Oct 4, 2010, 8:35:17 AM10/4/10
to django...@googlegroups.com
more like a python question :)

>>> li = ['a', 'b', 'c']
>>> ','.join(li)
'a,b,c'


Va salut,

CRISTEA, Adrian
Software developer




--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.


Sam Lai

unread,
Oct 5, 2010, 5:55:29 AM10/5/10
to django...@googlegroups.com
On 4 October 2010 21:41, ashy <ashwi...@gmail.com> wrote:
> Hi All,
>
> I am writing a django function in which I have a following list:
> li = ['a','b','c']
> I want to create a string from the list which should look like
> str = 'a,b,c'
>
> I plan to pass this string to not in () function of my sql query.

If you're using the Django ORM, you can do this without writing your own SQL.

model.objects.exclude(field__in=li)

Where model is your model class, and field is the name of the field
you want to apply the 'not in' filter on.

Reply all
Reply to author
Forward
0 new messages