iterate through a List code example
Example 1: how to loop over list
list = [1, 2, 3, 4, 5, 6]
for item in list:
print(item)
for index in range(len(list)-1):
print(list[index])
for index, value in enumerate(list):
print(value)
Example 2: how to iterate over a list in python
my_list = [1, 2, 3]
for item in my_list:
print(item)
Example 3: iterate through a list
my_list = ["Hello", "World"]
for i in my_list:
print(i)
Example 4: traversal of list in java
import java.util.ArrayList;
import java.util.Iterator;
public class IteratorSample {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<String>();
list.add("JavaFx");
list.add("Java");
list.add("WebGL");
list.add("OpenCV");
Iterator iterator = list.iterator();
while(iterator.hasNext()) {
System.out.println(iterator.next());
}
}
}
Example 5: iterate over a list python
list = [1, 3, 5, 7, 9]
for i in list:
print(i)