How to find and show the position of an item in the list, not setting the string value for the search?

Asked

Viewed 4,222 times

0

How to find and show the position of an item in the list, not setting the string value for the search?

Ex:

nome_pessoa = str(input('Informe o seu nome completo: '))

lista_nomes = nome_pessoa.split()

After the names are divided and transformed into a list, I would like to know how, for example, to indicate the position the third name(or item) is.

Suppose that in this first execution, the user has entered the name : 'Pedro Lucas Gomes'.

That way, after transforming this string into the list with the names, it will have the items: ['Pedro', 'Lucas', 'Gomes'']

What I want in exit data, that is to say, printado on screen is:

The name of the second position, which in this case would be 'Gomes', and the position number, which will be 2.

BUT CASE AT SECOND RUN, the user type the name 'João Neves Frufru Feitosa', what I want on output is:

The name of the second position is 'Frufru', and the number of its position is 2.

Did you understand? I won’t use the command:

print(list_names.index('Frufru'))

Because the name 'Frufru' will not be fixed in the code! The name requested in the program, will be typed from the keyboard by the user at the time of each code execution.

(I thank everyone who is striving to help and I apologize for my lack of clarity before the issue)

2 answers

0

Use "index" to pick the position of the item in the list.

lista_nomes.index('Gomes')

I don’t know if this is what you want, but this is the standard way to get an address on a list.

  • Thank you very much for your reply, my dear, but it is not what I want, but it was almost. What I want in data output is to show the name of a given position, and the number of the position of that item. Ex: If the person’s name is 'João Mota Silva Mufino', the name of the third position that will be shown in the terminal, will be "Mufino", and the number of the position of the list that I want the program to show, will be 3. I don’t know how to tell which position number, without defining the item name first. Set the item name in the code to show its position, is what I don’t want.

0

If you want to get the third item in the list, you should use brackets to access the item in the list you want. It’s important to remember that indexes start at 0, then to access the 3rd item, you must search for the index 2:

nome_completo = input('Por favor, informe seu nome completo: ')
lista_nome_completo = nome_completo.split()
print('O elemento 3 da lista é {}'.format(minha_lista[2]))

If you want to get the last name always, you should get the list size. A shortcut to the index is that -1 is the index of the last item.

nome_completo = input('Por favor, informe seu nome completo: ')
lista_nome_completo = nome_completo.split()
ultimo_elemento = len(lista_nome_completo) - 1
print('O último elemento da lista é o de número {}, e é {}'.format(ultimo_elemento, lista_nome_completo[-1]))
  • I am very grateful for the answer, but unfortunately it is not what I want. I may not have been able to be clear enough. I will use your code to try to clarify the situation: The name will be entered by the keyboard by the user, that is, it is not fixed. In this way, the user could enter 'João Mota Silva Mufino', in the next compilation. That way, what I would want to output is: "The third item on the list is : Mufino And it is in the position: 3" ?

  • @Pedrolucas96 I think you may have understood; look at the edition of the answer.

  • Peter, my namesake, now I’m sure the mistake was mine and the way I expressed myself. (I’m new here) But ok, we’re almost there, I’m going to use your code as a very direct example now: number of the position he is in? Did you understand better now? If by chance in the next execution of the program, I type the name 'Carlos Prado Matias Saraiva Junior Feitosa', I want to show the last pos, which will be 'Feitosa', and its number, 5.

  • Friend, why don’t you use matrix? Or how about a dictionary containing lists? Or the reverse.

  • @Pedrolucas96 The second example also shows the position of the last element.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.