'Skip items in a list range and continue with the rest

I want to print out some items in a list, but I want to skip a couple of items and continue with the rest.

This is an example of my code and how I do it so far.

mylist = ["dog","cat","lion","wolf","zebra","monkey","bear","eagle", "bison"]
for item in mylist:
    if item == mylist[4]:
        continue
    if item == mylist[5]:
        continue
    if item == mylist[6]:
        continue
    print(item)

How can I do the same thing but without using multiple if statements?



Solution 1:[1]

You can use an indexed for-loop (using the index to retrieve the element).

mylist = ["dog", "cat", "lion", "wolf", "zebra", "monkey", "bear", "eagle", "bison"]

for i in range(len(mylist)):
    if i in [4, 5, 6]:             # element indices to skip
        continue
    print(mylist[i])               # procedures desired

The alternative way to do so, is to modify the for-loop range, which would save two lines of code doing if. (This is a relatively bad idea)

mylist = ["dog", "cat", "lion", "wolf", "zebra", "monkey", "bear", "eagle", "bison"]

for i in (mylist[:4]+mylist[7:]):  # exclude the skipped ones
    print(mylist[i])               # procedures desired

Solution 2:[2]

You could utilize a list comprehension and remove undesired elements by using their indices.

mylist = ["dog", "cat", "lion", "wolf", "zebra", "monkey", "bear", "eagle", "bison"]
out = [elem for i, elem in enumerate(mylist) if i not in [4, 5, 6]]
print(out)

Output:

['dog', 'cat', 'lion', 'wolf', 'eagle', 'bison']

Solution 3:[3]

this is just another way to solve it:

mylist = ["dog","cat","lion","wolf","zebra","monkey","bear","eagle", "bison"]
banned=[mylist[4],mylist[5],mylist[6]]

#remove all the items that you  don't want before printing
for thing in banned:
    mylist.remove(thing)

#print every element in mylist now:
for animal in mylist:
    print (animal)

Solution 4:[4]

You can use slicing to get the specific indexes of a list. Try this:

for i in mylist[4:]:
    print(i)

This will print all the items after the 4th index. You can also try:

for i in mylist[4:7]:
    print(i)

This will print all index 4, 5 and 6

Solution 5:[5]

There are multiple ways to do that and one of them is this i write below:

mylist = ["dog","cat","lion","wolf","zebra","monkey","bear","eagle", "bison"]
skip_list = [my_list[4],my_list[5],my_list[6]]
for item in mylist:
    if item in skip_list:
        continue
    print(item)

here just you need to make a skip_list of the items you want to skip.

Solution 6:[6]

I'm not sure whether you want to skip elements on particular positions or elements with particular contents. For the former, you can try list comprehension,

if item in  [mylist[i] for i in [4,5,6]]:
    continue

For the latter, you can just put the elements in a list,

if item in  ["zebra","monkey","bear"]:
    continue 

Solution 7:[7]

This could be a solution:

mylist = ["dog", "cat", "lion", "wolf", "zebra", "monkey", "bear", "eagle", "bison"]

banned = [mylist[1], mylist[5], mylist[6]]

for item in mylist:
    if item in banned:
        continue
    else:
        print(item)

Solution 8:[8]

Heres another way to do the same

>>> mylist = ["dog","cat","lion","wolf","zebra","monkey","bear","eagle", "bison"]
>>> del mylist[4:6]
>>> mylist
['dog', 'cat', 'lion', 'wolf', 'bear', 'eagle', 'bison']

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3 alex p sp
Solution 4 The Pilot Dude
Solution 5 Cdaman
Solution 6
Solution 7 Tonechas
Solution 8 Chandan