OKPEDIA PYTHON LISTA EN

How to read a list in Python

To scroll and read all the elements in a python list, you can use a for loop.

for i in nomelista:
print(i)

The loop performs n iterations and reads the elements of the list from the first to the last.

  • Each iteration reads the nth element of the list and assigns it to the loop variable. In this case the variable i.
  • The name of the loop variable can be any (eg i, j, k, name, etc.).

Note. It is not necessary to indicate the number of elements to read the list. It is sufficient to indicate the name of the list. The cycle stops automatically after the last element of the list.
the reading of the list is sequential from index 0 to index n

Example

Make a list with 7 elements

lista=['a','b','c','d','e','f','g']

Read and print list items in sequential order by a for loop.

for i in lista:
print(i)

In each iteration the for loop reads the nth element of the list, assigns it to the variable i and prints it on screen by the statement print (i).

The output result is as follows:

a
b
c
d
e
f
g

The cycle reads in sequence all the elements of the list from the first to the last.

https://how.okpedia.org/en/python/how-to-read-a-list-in-python


Report us an error or send a suggestion to improve this page


Python List


FacebookTwitterLinkedinLinkedin