I don't want python print specified name in python

Question:

fruits = ["apple", "grape", "orange"]

for i in fruits:
    print(1*i)

So here I don’t want python print apple fruit I want him only print grape and orange fruits

Asked By: Ahmed WG

||

Answers:

If you want to print all but the first element in a list, you can use a list slice to specify everything in the list from index 1 onward to the end of the list.

for i in fruits[1:]:
    print(i)

You can also simply expand the slice into print, and specify the separator as a newline.

print(*fruits[1:], sep='n')
Answered By: Chris

I don’t have sure if it’s really what do you like to do:

fruits = ["apple", "grape", "orange"]

# list of string you dont want to print
dont_print = ["apple"]

for i in fruits:
    if i not in dont_print:
        print(1*i)

I used a list exceptions but there are other ways to.

Answered By: Pedro Henrique

You can use if to select cases that you don’t want to print. Like this:

fruits = ["apple", "grape", "orange"]

for i in fruits:
    if i == "apple":
         continue
    else:
        print(i)

If you want to include another rules you can use the logical operator in. Like this:

fruits = ["apple", "grape", "orange"]
remove_fruits = ["apple", "banana"]

for i in fruits:
    if i in remove_fruits:
         continue
    else:
        print(i)
Answered By: João Álex
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.