python - Print, by line, the letter and nickname append()

Asked

Viewed 115 times

0

Based on the following list::

escritores = [['Pedro', 'Tamen'], ['Almeida', 'Garrett'], ['Camilo', 'Pessanha'], ['Almada', 'Negreiros'], ['Ibn', 'Bassam'], ['Antonio', 'Aleixo'], ['Ricardo', 'Reis'], ['Mario', 'Sá-Carneiro'], ['Mario', 'Cesariny'], ['Luis', 'Camões'], ['Miguel', 'Torga'], ['Natália', 'Correia'], ['Tolentino', 'Mendonça']]

print("Lista Escritores\n",escritores,"\n")

I must create a list with the initial of name and the nickname of every writer.

Using the method append(element).

Print, by line, the letter and surname.

Print the new list.

Now my code being this

escritores = [['Pedro', 'Tamen'], ['Almeida', 'Garrett'], ['Camilo', 'Pessanha'], ['Almada', 'Negreiros'], ['Ibn', 'Bassam'], ['Antonio', 'Aleixo'], ['Ricardo', 'Reis'], ['Mario', 'Sá-Carneiro'], ['Mario', 'Cesariny'], ['Luis', 'Camões'], ['Miguel', 'Torga'], ['Natália', 'Correia'], ['Tolentino', 'Mendonça']]

print("Lista Escritores\n",escritores,"\n")

iniciais = [['P', 'Tamen'], ['A', 'Garrett'], ['C', 'Pessanha'], ['A', 'Negreiros'], ['I', 'Bassam'], ['A', 'Aleixo'], ['R', 'Reis'], ['M', 'Sá-Carneiro'], ['M', 'Cesariny'], ['L', 'Camões'], ['M', 'Torga'], ['N', 'Correia'], ['T', 'Mendonça']]

for nome, apelido in iniciais:
    print('{}{} - {} {}'.format(nome[0], apelido[0], nome, apelido))



print("\nNova Lista\n",iniciais)

I’m not getting the append.() nor am I realizing what you want!?!? Am I doing it all wrong!?!?

I must be, because I’m missing the append.()!!!!

  • 2

    The idea is to generate the list with initial and nickname dynamically (with a for for example), instead of creating it by hand.

1 answer

1


It’s almost there, but the variable iniciais it’s not making much sense, if you want to print that content you can do it in for:

escritores = [['Pedro', 'Tamen'], ['Almeida', 'Garrett'], ['Camilo', 'Pessanha'], ['Almada', 'Negreiros'], ['Ibn', 'Bassam'], ['Antonio', 'Aleixo'], ['Ricardo', 'Reis'], ['Mario', 'Sá-Carneiro'], ['Mario', 'Cesariny'], ['Luis', 'Camões'], ['Miguel', 'Torga'], ['Natália', 'Correia'], ['Tolentino', 'Mendonça']]

iniciais = []
for nome, apel in escritores:
  print(nome[0], apel)
  iniciais.append('{}, {}'.format(nome[0], apel))
print(iniciais) # ['P, Tamen', 'A, Garrett', 'C, Pessanha', 'A, Negreiros', 'I, Bassam', 'A, Aleixo', 'R, Reis', 'M, Sá-Carneiro', 'M, Cesariny', 'L, Camões', 'M, Torga', 'N, Correia', 'T, Mendonça']

DEMONSTRATION

  • Right! But I must create a list with the initial of each writer’s name and nickname. And make append().

  • 1

    @Joaopeixotofernandes, edited

  • 1

    What a master!! Thank you!! Excellent!!!! Thank you!.

  • You’re welcome @Joaopeixotofernandes

Browser other questions tagged

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