Home > OS >  I don't want python print specified name in python
I don't want python print specified name in python

Time:08-08

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

CodePudding user response:

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.

CodePudding user response:

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')

CodePudding user response:

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)
  • Related