By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,100 Members | 2,495 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,100 IT Pros & Developers. It's quick & easy.

converting a string into a list and vice versa

P: 54
If I have a loop that yields something like:

returnedlist = ['a', 'b', 'c',
'd', 'e', 'f']

how can I make the contents of returnedlist into a string called NewString that outputs something like

NewString= (a b c
d e f)

Same question for the reverse request.

Lastly, in regex can lists be manipulated, or is it a string-only kind of party?
Jul 27 '07 #1
Share this Question
Share on Google+
6 Replies


bartonc
Expert 5K+
P: 6,596
If I have a loop that yields something like:

returnedlist = ['a', 'b', 'c',
'd', 'e', 'f']

how can I make the contents of returnedlist into a string called NewString that outputs something like

NewString= (a b c
d e f)

Same question for the reverse request.

Lastly, in regex can lists be manipulated, or is it a string-only kind of party?
Expand|Select|Wrap|Line Numbers
  1. >>> returnedlist = list("abcdef")
  2. >>> returnedlist
  3. ['a', 'b', 'c', 'd', 'e', 'f']
  4. >>> " ".join(returnedlist)
  5. 'a b c d e f'
  6. >>> " !Wow! ".join(returnedlist)
  7. 'a !Wow! b !Wow! c !Wow! d !Wow! e !Wow! f'
  8. >>> 
Jul 27 '07 #2

P: 54
Okay, that was helpful but maybe i'm doing it wrong. Here's my real world application....
I have a script that when it prints (on the screen) it returns something that looks like...

[['name=Keywords\n', 'xmlns=""\n'], ['align=center>0.08</td><td\n', 'align=center><a href=/cc/3/83683.html>Listen</a></td></tr><tr><td>Acme Packet, Inc.</td><td><a href="http://finance.yahoo.com/q?s=apkt">APKT</a></td><td\n'], ['align=center>0.3</td><td\n', 'align=center><a href=/cc/7/82467.html>Listen</a></td></tr><tr><td>Agnico-Eagle Mines Limited</td><td><a href="http://finance.yahoo.com/q?s=aem">AEM</a></td><td\n'],

etc etc

that is when i type print stuff

But I want to do a few basic regex on it, and I thought my problem was that stuff is a list, not a string, so I tried this....

>>> " ".join(stuff)
>>> pattern = re.compile ("[A-Z]+")
>>> findings = re.findall(pattern, stuff)

When i press return i get....
>>> " ".join(stuff)
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
TypeError: sequence item 0: expected string, list found


Any idea why this is happening and what I can do to avoid it?

thanks
pc
Aug 1 '07 #3

ilikepython
Expert 100+
P: 844
Okay, that was helpful but maybe i'm doing it wrong. Here's my real world application....
I have a script that when it prints (on the screen) it returns something that looks like...

[['name=Keywords\n', 'xmlns=""\n'], ['align=center>0.08</td><td\n', 'align=center><a href=/cc/3/83683.html>Listen</a></td></tr><tr><td>Acme Packet, Inc.</td><td><a href="http://finance.yahoo.com/q?s=apkt">APKT</a></td><td\n'], ['align=center>0.3</td><td\n', 'align=center><a href=/cc/7/82467.html>Listen</a></td></tr><tr><td>Agnico-Eagle Mines Limited</td><td><a href="http://finance.yahoo.com/q?s=aem">AEM</a></td><td\n'],

etc etc

that is when i type print stuff

But I want to do a few basic regex on it, and I thought my problem was that stuff is a list, not a string, so I tried this....

>>> " ".join(stuff)
>>> pattern = re.compile ("[A-Z]+")
>>> findings = re.findall(pattern, stuff)

When i press return i get....
>>> " ".join(stuff)
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
TypeError: sequence item 0: expected string, list found


Any idea why this is happening and what I can do to avoid it?

thanks
pc
The join function doesn't change the list in place. It return a string seperate from the list. You call join but never set the return value to a variable.
Expand|Select|Wrap|Line Numbers
  1. stuff = "".join(stuff)
  2.  
Aug 1 '07 #4

bvdet
Expert Mod 2.5K+
P: 2,851
Okay, that was helpful but maybe i'm doing it wrong. Here's my real world application....
I have a script that when it prints (on the screen) it returns something that looks like...

[['name=Keywords\n', 'xmlns=""\n'], ['align=center>0.08</td><td\n', 'align=center><a href=/cc/3/83683.html>Listen</a></td></tr><tr><td>Acme Packet, Inc.</td><td><a href="http://finance.yahoo.com/q?s=apkt">APKT</a></td><td\n'], ['align=center>0.3</td><td\n', 'align=center><a href=/cc/7/82467.html>Listen</a></td></tr><tr><td>Agnico-Eagle Mines Limited</td><td><a href="http://finance.yahoo.com/q?s=aem">AEM</a></td><td\n'],

etc etc

that is when i type print stuff

But I want to do a few basic regex on it, and I thought my problem was that stuff is a list, not a string, so I tried this....

>>> " ".join(stuff)
>>> pattern = re.compile ("[A-Z]+")
>>> findings = re.findall(pattern, stuff)

When i press return i get....
>>> " ".join(stuff)
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
TypeError: sequence item 0: expected string, list found


Any idea why this is happening and what I can do to avoid it?

thanks
pc
'stuff' is a list of lists. You can only join a list of strings.
Expand|Select|Wrap|Line Numbers
  1. strList = []
  2. stuff = [['name=Keywords\n', 'xmlns=""\n'], ['align=center>0.08</td><td\n', 'align=center><a href=/cc/3/83683.html>Listen</a></td></tr><tr><td>Acme Packet, Inc.</td><td><a href="http://finance.yahoo.com/q?s=apkt">APKT</a></td><td\n'], ['align=center>0.3</td><td\n', 'align=center><a href=/cc/7/82467.html>Listen</a></td></tr><tr><td>Agnico-Eagle Mines Limited</td><td><a href="http://finance.yahoo.com/q?s=aem">AEM</a></td><td\n']]
  3. for item in stuff:
  4.     for i in item:
  5.         strList.append(i)
  6.  
  7. outStr = " ".join(strList)
  8. print outStr
Output:
>>> name=Keywords
xmlns=""
align=center>0.08</td><td
align=center><a href=/cc/3/83683.html>Listen</a></td></tr><tr><td>Acme Packet, Inc.</td><td><a href="http://finance.yahoo.com/q?s=apkt">APKT</a></td><td
align=center>0.3</td><td
align=center><a href=/cc/7/82467.html>Listen</a></td></tr><tr><td>Agnico-Eagle Mines Limited</td><td><a href="http://finance.yahoo.com/q?s=aem">AEM</a></td><td

>>>
Aug 2 '07 #5

P: 54
BVDET,

Your solution works perfect. I'd like to just try and reitterate what it does in english to make sure I have a firm grasp, so that I don't ask the same question in the future.

Essentially my scrape returned a lists of lists. But I wanted a list of strings.

So the first thing you did is create a new list called strList.

Then the FOR loop had it go over each item in the list of lists and put them into strList.

When it put it into strList, it was put in as a string by default (?).


From there, because the elemants in the original list of lists were now saved as a list of strings, I could do join function.

Right...?

thanks
pc
Aug 3 '07 #6

bvdet
Expert Mod 2.5K+
P: 2,851
BVDET,

Your solution works perfect. I'd like to just try and reitterate what it does in english to make sure I have a firm grasp, so that I don't ask the same question in the future.

Essentially my scrape returned a lists of lists. But I wanted a list of strings.

So the first thing you did is create a new list called strList.

Then the FOR loop had it go over each item in the list of lists and put them into strList.

When it put it into strList, it was put in as a string by default (?).


From there, because the elemants in the original list of lists were now saved as a list of strings, I could do join function.

Right...?

thanks
pc
Yes. The nested lists contained only strings, and each element remained a string when appended to strList.
Aug 3 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.