[vc_row css_animation=”” row_type=”row” use_row_as_full_screen_section=”no” type=”full_width” angled_section=”no” text_align=”left” background_image_as_pattern=”without_pattern”][vc_column][vc_column_text]

Loops and Array in Python

[/vc_column_text][vc_separator type=”normal” up=”1″][vc_column_text css=”.vc_custom_1571149895584{background-color: #f6f6f6 !important;}”]

By using loops in Python we can iterate all the elements of an Array, and therefore manage the values in each of the elements

[/vc_column_text][vc_empty_space height=”20px”][vc_column_text]

How to process all elements of an array in Python?

Okay, now you have created your Array in Python and want to start using it. But how do we access the elements in the Array in a easy way? It’s actually really simple, we just use a loop. For example, maybe you want to go through all the values in the array to find a particular value. You can iterate through all the values in an array using a loop. If you need to have a look at how the loop in Python is created and used, have a look at this page.

 

In Python, it is possible to iterate, go through all elements of an array using a loop

[/vc_column_text][vc_empty_space height=”30px”][vc_column_text]

[/vc_column_text][vc_empty_space height=”10px”][vc_separator type=”normal” thickness=”2″][vc_empty_space height=”10px”][vc_column_text]

Example – How to process elements of an array in Python

Let’s take a example on how to create a loop that process the elements of an array in Python. We will use our favorite array – the one that contains animals.

 

Start by creating the Array (remember how we did that?)

 

animal_arr = ["Dog", "Shark", "Cat", "Horse", "Snake"]

Okay, now that we have our array, we want to use a loop to print all the values in animal_arr

 

for animals in animals_arr:
    print(animals)

The results is:

 

Dog
Shark
Cat
Horse
Snake

 

You can also select each index individually and get exactly the same result:

 

for i in range(len(animal_arr)):
    print(animal_arr[i])

The result in this case is exactly the same as the previous one

 

Dog
Shark
Cat
Horse
Snake

[/vc_column_text][vc_empty_space height=”20px”][vc_column_text]

[/vc_column_text][vc_empty_space height=”10px”][vc_separator type=”normal” thickness=”2″][vc_empty_space height=”30px”][/vc_column][/vc_row][vc_row css_animation=”” row_type=”row” use_row_as_full_screen_section=”no” type=”full_width” angled_section=”no” text_align=”left” background_image_as_pattern=”without_pattern”][vc_column][vc_column_text]

Previous Page    |    Next Page

[/vc_column_text][vc_empty_space][/vc_column][/vc_row][vc_row css_animation=”” row_type=”row” use_row_as_full_screen_section=”no” type=”full_width” angled_section=”no” text_align=”left” background_image_as_pattern=”without_pattern”][vc_column][/vc_column][/vc_row]