List problem

K

kevndcks

For example i write the following code in the Python command line;

Then enter this command, which will then return the following;

['One,Two,Three,Four']


Now the problem, reading through the Python tutorial's, it describe's
that list's can sliced, concatenated and so on etc

So i write the following

Then the error returned is that 'str' and 'list' could not be
concatenated which is where it baffles me. I'm understanding that this
mean's that the string 'Five' could not be joined onto the list, as i
havn't defined it in the array. Viewing the Python tutrial they have
the following, and working code;
['spam', 'eggs', 'bacon', 4]

How can they have the string 'bacon' and have it returned with no
error?
 
F

Fredrik Lundh

For example i write the following code in the Python command line;
list = ['One,Two,Three,Four']

Then enter this command, which will then return the following;

['One,Two,Three,Four']


Now the problem, reading through the Python tutorial's, it describe's
that list's can sliced, concatenated and so on etc

So i write the following

Then the error returned is that 'str' and 'list' could not be
concatenated which is where it baffles me.

if you got that error, you clearly didn't type what you say you typed:
>>> list = ['One,Two,Three,Four']
>>> list ['One,Two,Three,Four']
>>> list[1:] + ['Five']
['Five']

(note that the first list contains a single string; if you want to put
multiple strings in a list, you need to be more careful with where you
put the quotes.)

</F>
 
L

Larry Bates

For example i write the following code in the Python command line;
list = ['One,Two,Three,Four']

Then enter this command, which will then return the following;

['One,Two,Three,Four']


Now the problem, reading through the Python tutorial's, it describe's
that list's can sliced, concatenated and so on etc

So i write the following

Then the error returned is that 'str' and 'list' could not be
concatenated which is where it baffles me. I'm understanding that this
mean's that the string 'Five' could not be joined onto the list, as i
havn't defined it in the array. Viewing the Python tutrial they have
the following, and working code;
a[:2] + ['bacon', 2*2]
['spam', 'eggs', 'bacon', 4]

How can they have the string 'bacon' and have it returned with no
error?
Your error message doesn't match your command. Now if you
typed:

list[0]+['Five']

that gives you the error you showed.

What you meant to type was:

l = ['One','Two','Three','Four']

This is a list of four strings, what you entered was a list of one
string.

NOTE never call a variable 'list' as it will mask the built-in list
method (same goes for str, tuple, int, float, etc).

-Larry Bates
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,122
Latest member
VinayKumarNevatia_
Top