How to get the last item of a list in python
Question:
I started learning python a few day ago
I tried to make program of anime watchlist. I tested to include also adding new ones but when I’m trying to print out a new, added one, I am only getting a letter. How I can get a full text? There’s code
anime_list = ["one piece","fullmetal alchemist", "my hero academia", "oregairu", "jojo", "jujutsu kaisen"]
a_o_s = input ("What do you want to do ('add anime' or 'what I should watch?')n")
if a_o_s == "add anime":
anime_list = input ("What anime do you want to add?n")
print("thank you " + anime_list[anime_list -1] + " is added")
I tried too to just print every letter from the start of a name to an end but it doesn’t work either
anime_list = ["one piece","fullmetal alchemist", "my hero academia", "oregairu", "jojo", "jujutsu kaisen"]
a_o_s = input ("What do you want to do ('add anime' or 'what I should watch?')n")
if a_o_s == "add anime":
anime_list = input ("What anime do you want to add?n")
n_w = anime_list.count
print("thank you " + anime_list[n_w:-1] + " is added")
Is there any way to do it?
thank you
Answers:
When you do anime_list = input ("What anime do you want to add?n")
you redefine anime_list
. Now it’s not a list anymore but a string (the return value from input
). You have to append the data to the list. The last element can be accessed at index -1.
anime_list = ["one piece", "fullmetal alchemist", "my hero academia", "oregairu", "jojo", "jujutsu kaisen"]
anime_list.append(input("What anime do you want to add?n"))
print(f"thank you, {anime_list[-1]} was added")
when you want to get the last item of a list you can simply write name_of_your_list[-1] so for this instance you can do:
anime_list[-1]
which will give you the last item in the list
I started learning python a few day ago
I tried to make program of anime watchlist. I tested to include also adding new ones but when I’m trying to print out a new, added one, I am only getting a letter. How I can get a full text? There’s code
anime_list = ["one piece","fullmetal alchemist", "my hero academia", "oregairu", "jojo", "jujutsu kaisen"]
a_o_s = input ("What do you want to do ('add anime' or 'what I should watch?')n")
if a_o_s == "add anime":
anime_list = input ("What anime do you want to add?n")
print("thank you " + anime_list[anime_list -1] + " is added")
I tried too to just print every letter from the start of a name to an end but it doesn’t work either
anime_list = ["one piece","fullmetal alchemist", "my hero academia", "oregairu", "jojo", "jujutsu kaisen"]
a_o_s = input ("What do you want to do ('add anime' or 'what I should watch?')n")
if a_o_s == "add anime":
anime_list = input ("What anime do you want to add?n")
n_w = anime_list.count
print("thank you " + anime_list[n_w:-1] + " is added")
Is there any way to do it?
thank you
When you do anime_list = input ("What anime do you want to add?n")
you redefine anime_list
. Now it’s not a list anymore but a string (the return value from input
). You have to append the data to the list. The last element can be accessed at index -1.
anime_list = ["one piece", "fullmetal alchemist", "my hero academia", "oregairu", "jojo", "jujutsu kaisen"]
anime_list.append(input("What anime do you want to add?n"))
print(f"thank you, {anime_list[-1]} was added")
when you want to get the last item of a list you can simply write name_of_your_list[-1] so for this instance you can do:
anime_list[-1]
which will give you the last item in the list