[Tutor] Python equivalent of Java Vecor?
Daniel Yoo
dyoo@hkn.eecs.berkeley.edu
Mon, 2 Oct 2000 19:48:11 -0700 (PDT)
> "Does Python have something equivalent to Java's Vector? For instance,
> most of the time if you want to use an array you have to declare the
> size up front. Java has a Vector class which is an array of objects
> which grows as you add things to it.
Sure --- Python has as a basic type the list. Here's how to construct an
empty list:
mylist = []
Getting the length it easy too:
print len(mylist)
Appending to it is similar to Java:
mylist.append(42)
mylist.append("Hello")
Getting at an arbitrary element is like an array operator:
###
>>> mylist = ['this', 'is', 'a', 'test']
>>> mylist[1]
'is'
###
Doing an iteraton over a list uses a simple 'for' loop:
for element in mylist:
print element
I'm not sure what the consensus is on making a presizing array --- I use
this:
mylist = [0] * 10
to get a zeroed list of 10 elements.
Good luck!