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

Alot of issues with Python List and Split Exercise

Hi, this is my first post. I'm working the Python track, which I'm doing ok so far.

For some reason the list and split functions gave me alot of issues. For the exercise, I had to look at someone else's code to get past it and I still don't understand how it works. Or if I had to, I would have alot of trouble explaining it back to someone.

Can anyone give advise on a better way to learn this? Or additional exercises?

thanks

Chris

4 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,468 Points

Let's look at the str (string) method split() (docs), is a way to break a string into parts base on some character(s) in the string being the divide points. The split() method returns a list of the string pieces. The characters used to divide the string are not returned. For example:

In [1]: my_string = 'aaaXbbbXcccXddd'

In [2]: lst = my_string.split('X') # split on capitol X

In [3]: lst
Out[3]: ['aaa', 'bbb', 'ccc', 'ddd']

In [4]: my_string = 'aaa, bbb, ccc, ddd'

In [5]: lst = my_string.split(', ') # split on comma space

In [6]: lst
Out[6]: ['aaa', 'bbb', 'ccc', 'ddd']

In [7]: 'aaadcbbbbdcbcccdcbddd'.split('dcb') # split on the substring 'dcb'
Out[7]: ['aaa', 'bbb', 'ccc', 'ddd']

In [8]: 'aaa    bbb ccc          ddd'.split() # split on default whitespace
Out[8]: ['aaa', 'bbb', 'ccc', 'ddd']

List is a very wide subject. What aspect of lists are troublesome?

I tried to post a rebuttal yesterday however the response kept timing out.

Thanks for the response above. This is what I need, more examples.

Are there any more examples for list and split that I can use for Python so I can get better at it before moving onto the next lesson?

Also the code brackets are hard to see above and in the exercises. Any way to fix this?

thanks

Chris Freeman
Chris Freeman
Treehouse Moderator 68,468 Points

Treehouse was under maintenance last night 8pm-10:30pm PST.

The best way to grasp more on lists is to play with the code. Try the using python in interactive mode by typing python at the command or shell prompt. Then look at examples in Python structures tutorial.

I hear you on the hard-to-read formatting. There isn't a way to change it that I know of. I wish there was.

I don't get why to play with the Python code in interactive mode, what is the objective here? What is this trying to teach me in being a better Python coder? Missing the why here.

There needs to be more examples. If Treehouse can't provide them, then point us to some useful URLs where we can learn.

I feel like I'm back in college taking programming classes, where is one example, maybe two and your expected to crank out some code. I understand that each lesson is designed to build off of each other, however if you can't get the basics down of a lesson and not able to code it up with out much difficulty or teach someone else, then this teaching approach isn't very useful and a different method needs to be used.

Chris Freeman
Chris Freeman
Treehouse Moderator 68,468 Points

Sorry, I don't have a list of examples. Perhaps Kenneth Love can provide some direction.

With the topic of "lists" being so broad, it's hard to provide examples without knowing what aspect needs explaining. Do you need help with syntax of using lists in various ways, or given a list how can you manipulate it using the build-in methods. There's also list split notation: [start:stop:step]. So much to cover.

The more specifically you can describe what you don't understand the easier it will be to help you along. No question is too basic. Can you narrow it down some?

After years of programming, I am still constantly going to the interactive prompt to test code snippets especially when reviewing Community questions and my answers before posting them. I use the interactive prompt every day. I use the ipython interactive shell (as seen in my answer). Its more powerful than the basic interactive python interface. It's easy to try:

# type python at a prompt
$ python
Python 3.4.0 (default, Jun 19 2015, 14:20:21) 
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

At the >>> prompt, you can type can any python code and see how it works:

>>> lst = [1, 2, 3, 4]
>>> len(lst)
4
>>> lst[0]
1
>>> lst[3]
4
>>> lst[-1]
4

I'm familiar with the Python Interactive Shell. I'm running it (Python 2.7.5) on a Linux VM (CentOS 7) in VirtualBox and this was my formal intro to Python a few years back.

Let me try one of the exercises and see what type of output I get. If I get stuck, I will ask for help here.

Also for the List in Python, I can try to reference some of the pdf documents that I have and again, if there are issues, I will post back.

thanks