I am out of trial and error again Lists

sohcahtoa82 at gmail.com sohcahtoa82 at gmail.com
Wed Oct 22 17:35:18 EDT 2014


On Wednesday, October 22, 2014 2:06:35 PM UTC-7, Seymore4Head wrote:
> On Wed, 22 Oct 2014 16:57:00 -0400, Joel Goldstick
> <joel.goldstick at gmail.com> wrote:
> 
> >On Wed, Oct 22, 2014 at 4:30 PM, Seymore4Head
> ><Seymore4Head at hotmail.invalid> wrote:
> >> def nametonumber(name):
> >>     lst=[""]
> >>     for x,y in enumerate (name):
> >>         lst=lst.append(y)
> >>     print (lst)
> >>     return (lst)
> >> a=["1-800-getcharter"]
> >> print (nametonumber(a))#18004382427837
> >>
> >>
> >> The syntax for when to use a () and when to use [] still throws me a
> >> curve.
> >() is tuples which are immutable which means that the items can't be
> >changed.  [] is list which means that each item can be changed.
> >Tuples are useful because they can be used as keys in dictionaries and
> >are guarantied not to change.  Lists are useful because they can be
> >updated.
> >
> >What you are doing confuses me.  You don't use x, which is the enumerated value.
> >
> >FIrst lst should be lst = [] .  You don't need to set the first
> >element in the list to an empty string.  You just want to establish
> >that you have an empty list called lst
> >Second, you don't need lst = lst.append(y) because you can just say
> >lst.append(y).  This will append the y value to the end of the list.
> >As to converting letters to the corresponding numbers on a phone
> >keypad, you don't show you code here for that
> >>
> >> For now, I am trying to end up with a list that has each character in
> >> "a" as a single item.
> >>
> >> I get:
> >> None
> >> None
> >> --
> >> https://mail.python.org/mailman/listinfo/python-list
> 
> The lst=lst.append(y)
> Was the mistake I never could see.
> 
> I am using enumerate just for practice.  To me that is just as easy as
> typing len(something) and it seems more flexible.
> 
> and.......the reason I don't show the code for the conversions is that
> I haven't got that far yet.  :)
> 
> Thank you

I'm still confused as to why you're using enumerate.  Using it when you don't need to for "practice" just seems strange.  You don't even need to use len(something) in your case.  You should just be using 'for y in name:' if you don't need that x.  Enumerate is essentially just a shortcut to zipping a range based on the length.  For example...

for x, y in enumerate(name):

is equivalent to:

for x, y in zip(range(len(name)), name):

And both are pointless if you're not using the x.

Also, is there a reason why you're defining 'a' to be a list with a single string value, rather than just defining it as a string?  It seems like you should probably just have:

a = "1-800-getcharter"



More information about the Python-list mailing list