In this chapter of the Python tutorial, let us create an example to demonstrate how to use Python lists. Python list is just like an array where various items have been kept inside it and any particular item can then be retrieved by using the index of the list, besides that, Python lists also allow us to mix different types of items within a list, for example, you can present a list consisting of all numbers such as below…
numlist = [1,2,3]
But you also can present a list consisting of various data types such as the following:-
li = [1,True,"hello"]
If you want to retrieve an item within the python list then all you need to do is to use the index of the list as follows:-
li = [1,True,"hello"] li[0] # 1, remembered that the index of a list is always started with 0!
You can create a list with the above method but you can also create a list with the list constructor as follows:
alist = list(("Hello", "World", "!"))
Now let us look at an example of how to loop through a Python list and print out those items within the list one by one!
alist = list(("Hello", "World", "!")) for word in alist: print(word)
Hello World !
Basically, the program above used the for loop to print out each element within the list object.