To get the number of items in a list, it's len(List)
That'll return an integer value
To access an item in the list, you can List[index]
Now when you do the length function, if the list has 4 items, they "occupy" the index locations 0, 1, 2, 3.
So you can iterate (or loop) over the items in the list 0 -> 3.
Personally from there I'd use this method:
Quote:
for index, item in enumerate(L):
print index, item
It's saying (essentially) for every item inside the list, get the index position of the item in the list... as well as the item itself.
So what you could do is:
Code:
for index, item in enumerate(listA):
listA[index] = 2 * item
There's a variety of ways to loop, so you could just do:
Code:
for item in listA:
item = item * 2
Course, not overly familiar with p*thon, but if it's returning a reference to the item, this should be fine.
You can use the iterator, but I am not sure how the iterator works within python, calling the next method is fine whilst something is there to call it upon... but I don't know how you'd check.
In java the iterator has a method called "hasNext()" which returns a boolean value if there are more objects to iterate over.
So get the iterator, then enter a loop with the condition: (remember, this is java - not p*thon. Tis an example).
Code:
while(iterator.hasNext()){
}
inside the loop you'd know there are more items to iterate over (hasNext returned true), and therefore can call the iterators next method to return a reference to the item. Calling the next method not only returns the object, or reference to, it also moves the iterator forward, so the next call to "hasNext()" will ascertain if there's any more.
In this instance I have no idea what p*thon does with its iterator to check there's anything left to iterate over, so therefore it's easier to use a more conventional method - just a basic loop through the indexes of your list, and access the elements via their index to manipulate them. I think every language has this feature, so it's pretty universal.