Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Introducing Lists Using Lists Review Split and Join

eestsaid
eestsaid
1,311 Points

Lists, strings, split and join

There was a review question that picked up a comment from the video. It reads... "If I had a list and I wanted to turn it into a string by combining each value together with a specified separator. What method would I use?"

The answer reads ... "split. Its on strings" and the comment reads ... "Well done! That's right! It does seem like it might be on lists, but indeed it is off of strings, and you can join any iterable together."

I don't understand what is being referred to here particularly "...It does seem like it might be on lists, but indeed it is off of strings, and you can join any iterable together."

Thanks

1 Answer

Ryan Dsouza
Ryan Dsouza
9,388 Points

I believe it's being referred to the way data is stored and retrieved to and from strings.

Example - storing array values into strings can be done by

int[] list = new int[10];
StringBuilder str = new StringBuilder();
for (int i = 0; i < list.length; i++) {
    str.append(list[i]).append(",");
}
System.out.print("string", str.toString());

and loading/parsing from a string back to an array can be done by

StringTokenizer st = new StringTokenizer(savedString, ",");
int[] savedList = new int[10];
for (int i = 0; i < 10; i++) {
    savedList[i] = Integer.parseInt(st.nextToken());
}
eestsaid
eestsaid
1,311 Points

Thanks Ryan. Bit over-my-head. Could you provide a simpler example?