Lists in PYTHON

0

I'm using lists to do a program. Initially I wanted to make a program that would create as many variables as one wanted, but I ended up finding an easier way. I thought about creating a list and adding 10 elements to it. After that the program would have to read element by element to execute the next step of the program (I can not reveal the next step), but I came across a problem, how to make the program read the separate lines of a list? For example:

Lista = [1, 2, 3, 4, 5]

My program would pick up item by item, in case it would get the item from list L, 1, and execute the rest of the program, later it would take item 2 and execute, continuing until it reaches number 5. There is any command that allows me to grab the item from the list in the position I want and display it on the screen? Or allow you to read line by line from the list? How to solve my problem? I've tried everything!

    
asked by anonymous 01.04.2018 / 05:27

2 answers

1

Use lista[0] to get the first element, lista[1] to get the second, and so on. To know the size of a list use the len() function.

lista = [1, 2, 3, 4, 5]

print(lista[0])
print(lista(1])
print(len(lista))

Another thing, in Python, avoid creating variables with the first character in uppercase, that is, use lista instead of Lista . The use of the first letter in upper case is reserved for the definition of the classes (Python does not complain but can cause some confusion).

    
01.04.2018 / 14:04
0

You can try to use the for loop.

For example.

lista = [1, 2, 3, 4, 5]

for item in lista:
    print(item) # item é o número contido na lista que vc quer usar.

    #Aqui vc coloca o resto do programa

Translating: For each item in the list:     he will start the (item)

Or you can do this:

lista = [1, 2, 3, 4, 5]
n = len(lista)

for i in range(0, n):
    item = lista[i] # item é o número contido na lista que vc quer usar.
    print(item) 
    #Aqui vc coloca o resto do programa

It's practically the same as the other, but in a less lean way.

    
12.04.2018 / 17:39