assigning values to array element

Robert Brewer fumanchu at amor.org
Tue Oct 14 22:00:59 EDT 2003


Well, Ben, where do we start?

First of all, you're not working with an array, there. You're working
with a mutable list. The items in that list are 'Peter', 'Sam', 'Dave',
and 'Carl'; they are not variable names. There are no indexes into the
list except by number and iteration. In other words, you can obtain m[0]
or "for o in m".

If you want named keys, use a dictionary instead of a list:

m = {'Peter': 0, 'Sam': 0, 'Dave': 0, 'Carl': 0}
for i in m:
  # Here first i is 'Peter'..
  m[i] = 10

If that's not what you want, but instead you want to change the first
item in the list from 'Peter' to 10, you can write:

m[0] = 10

HTH!


Robert Brewer
MIS
Amor Ministries
fumanchu at amor.org




> -----Original Message-----
> From: Ben [mailto:crescent_au at yahoo.com] 
> Sent: Tuesday, October 14, 2003 6:35 PM
> To: python-list at python.org
> Subject: assigning values to array element
> 
> 
> Hi all,
> 
> This may sound easy but I'm having trouble assigning values to array
> element. My problem is as follows:
> 
> m = ['Peter', 'Sam', 'Dave', 'Carl']
> for o in m:
>   # Here first o is 'Peter'.. I want to do something like this:
>   Peter = 10
>   
>   # if i do %s %o = 10, it gives me error...
> 
> How can I do it?
> 
> Thanks
> Ben
> -- 
> http://mail.python.org/mailman/listinfo/python-list
> 





More information about the Python-list mailing list