How do I get datalist to string?

>>> days = 24/6
>>> print days
4
>>> hours = 45/7
>>> print hours
6
>>> datalist = []
>>> datalist.append(days)
>>> datalist.append(" days ")
>>> print datalist
[4, ' days ']
>>> datalist_str = "".join(datalist)
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
TypeError: sequence item 0: expected string, int found

Recommended Answers

All 8 Replies

Pretty easy, I came up with this one liner for it:

print " ".join(["%s" % el for el in datalist])

Hope that helps. : D

  1. print " ".join(["%s" % el for el in datalist])

This means, take take el from datalist, convert it to string "%s" % el, and put into the list.
Correct me if I am wrong.

How do I pass the strings in a list to separate variables ?
For example:

short_list =

I want the following variables to store the individual short_list values as shown:

var_a = 'a'
var_b = 'b'
var_c = 'c'

Thanks!
iamnew

Next time please push start a new thread button from the Python groups main list so your post will be better seen.

I push this (code) here, that gives nice looking fixed width text and keeps the white space, which is so essential for Python. Use also that next time when you some code stuff.

This is from interactive Python prompt as your request was so simple.

>>> short_list = ['a\n' , 'b\n', 'c\n']
>>> var_a, var_b, var_c = [ x.rstrip() for x in short_list ] ## rstrip newline out and assign
>>> var_a, var_b, var_c
('a', 'b', 'c')
>>> 

It's a trick question because you don't store the values in a separate variable (that's why we have lists), you use
short_list[0].strip()
short_list[1].strip()
short_list[2].strip()
or strip all of the values in short_list if you don't want to strip() each time.

short_list = ['a\n' , 'b\n', 'c\n']
for x in range(len(short_list)):
   short_list[x] = short_list[x].strip()

Thank you! This exactly the way I wanted, I can now proceed with further scripting, :).

Next time please push start a new thread button from the Python groups main list so your post will be better seen.

I push this (code) here, that gives nice looking fixed width text and keeps the white space, which is so essential for Python. Use also that next time when you some code stuff.

This is from interactive Python prompt as your request was so simple.

>>> short_list = ['a\n' , 'b\n', 'c\n']
>>> var_a, var_b, var_c = [ x.rstrip() for x in short_list ] ## rstrip newline out and assign
>>> var_a, var_b, var_c
('a', 'b', 'c')
>>>

end quote.

You are welcome but consider that generally it is not so much necessary put list
elements to different variables if you use lists properly.

Consider therefore also this using [index] to get the elements. In that case be careful in that index goes from 0 until length-1.

short_list = [ x.rstrip() for x in short_list ] ## rstrip newline out
",".join(map(str, (1,2,3,4)))
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.