Lists in python are just like an array in Java, a python list is liked an apartment consists of many rooms where you will find a person living inside each of those rooms. The only difference between the Python list and the Java array is that Java is very strict when it comes to the type of element which is allowed to stay inside a particular type of array which means if that array belongs to the integer type then only integer will be allowed to stay inside that array, whereas Python will allow every data types to stay together inside a single list. For example, in the below program, we will put a string, a number, and a dictionary within that same list and then use those data types according to our needs.

one = 1
hello = "That beautiful world!"
some_dictionary = {"one" : 1, "two" : 2, "three" : 3}

world_list = [one, hello, some_dictionary]


The outcome from the above python program

In order to access an element within a list, we can use either one of the following statements:

some_list = [1, 2, 3]
# the first index of a list is 0
one = some_list[0] # assign the first element within that list to the left hand side variable
one_one = some_list.pop(0) # assign the first element within that list to the left hand side variable and then remove that element from the list

In order to find out what is the index number of an element within a list, we will use the below method.

one = 1 # assign 1 to one
two = 2 # assign 2 to two
list_one = ["hello", "world"]
list_two = [1, 2, 3]
list_three = [one, two]

print(list_one.index("hello")) # output 0
print(list_two.index(2)) # output 1
print(list_three.index(two)) # output 1 

The lists in python is a very complex topic, therefore, we will break it into a smaller group and continue to talk about the same subject in the next chapter.

I have updated the previous tutorial and corrected a few statements within that tutorial, you can read the updated version through this link! We all are not perfect therefore sometimes we do make mistake, if you find anything that is not right in any of the Python tutorial posts do let me know in the comment section below that post so I can correct the mistake immediately on that same post, thank you.

Please follow and like us:

Leave a Reply

Your email address will not be published. Required fields are marked *