Last modified: Feb 15, 2023 By Alexander Williams

How to Skip a Value in a List python

Python provides many ways to skip a value in a list. In this article, we learn how to :

  1. Skip a value in a list by value
  2. Skip multi values in a list
  3. Skip a value in a list by type
  4. Skip a value in a list by index

Let's get started

Skip a value in a list by value

To skip a value in a list, we need to use the if condition to check the value we want to ignore and continue to skip it. In the following example, we'll skip the Html value in a my_list:

my_list = ["Python", "Django", "Html", "Javascript", "Java"] # 👉️ List

for i in my_list: # 👉️ Loop Over my_list
    if i == "Html": # 👉️ Check if item value is "Html"
        continue # 👉️ Skip
    else: # 👉️ If not
        print(i)

Output:

Python
Django
Javascript
Java

As you can see, "Html" is not printed.

Another way to skip a value is using for loop and if condition in the inline syntax,

my_list = ["Python", "Django", "Html", "Javascript", "Java"] # 👉️ List

new_list = [i for i in my_list if i != "Html"] # 👉️ New list

for i in new_list: # 👉️ Loop Over my_list
   print(i) # 👉️ Print item

Output:

Python
Django
Javascript
Java

However, both examples are pretty good. You can choose whatever you like. In the next part, we'll see how to skip multi values.

Skip multi values in a list

We have two ways to skip multi values in a list. The first is to skip by the OR keyword and the second by another list: items.

In the first example, we'll use the OR condition to skip "Html" and "Javascript" in my_list, 

my_list = ["Python", "Django", "Html", "Javascript", "Java"] # 👉️ List

for i in my_list: # 👉️ Loop over my_list
    if i == "Html" or i == "Javascript": # 👉️ Check using OR
        continue
    else:
        print(i)

Output:

Python
Django
Java

In the next example, we'll skip all skip_list items in my_list.

my_list = ["Python", "Django", "Html", "Javascript", "Java"] # 👉️ List

skip_list = ["Html", "Javascript"] # 👉️ List of values to skip

for i in my_list: # 👉️ Loop over my_list
    if i in skip_list: # 👉️ If item of m_list is in skip_list
        continue
    else:
        print(i)

Output:

Python
Django
Java

As you can see, Html and Javascript have been skipped.

Skip a value in a list by type

To skip a value in a list by type, we can use type() and isinstance() built-in functions.

type() returns the type of a given object. However, In the example below, we will use type() to skip integers in my_list.

my_list = ["Python", "Django", 15, "Html", 33, "Javascript", "Java"] # 👉️ List

for i in my_list:  # 👉️ Loop over my_list
    if type(i) is int: # 👉️ Check For integer items
        continue
    else:
        print(i)

Output:

Python
Django
Html
Javascript
Java

For more information about the type(), I recommended visiting How to get the type of a variable.

isinstance() return True if the given object is the given type. Otherwise returns False. In the example below, we'll use isinstance() to skip integers value in my_list.

my_list = ["Python", "Django", 15, "Html", 33, "Javascript", "Java"] # 👉️ List

for i in my_list:  # 👉️ Loop over my_list
    if isinstance(i, int): # 👉️ Check if item is an integer
        continue
    else:
        print(i)

Output:

Python
Django
Html
Javascript
Java

Check the Type of Variable in Python for more information about isinstance().

Skip a value in a list By index

To skip a value in a list by index, we can use enumerate() to add a counter to the list. However, let's see an example.

my_list = ["Python", "Django", "Html", "Javascript", "Java"] # 👉️ List

skip_index = 4 # 👉️ Index to skip

for index, i in enumerate(my_list): # 👉️ Loop Over my_list
    if index == skip_index: # 👉️ Check Index
        continue
    else:
        print(i)

Output:

Python
Django
Html
Javascript

As you know, the list index starts with 0. And we have skipped index 4 (index 4 = "Java").

Another way for skipping by the index is using pop(). pop() removes the item from a list by specific position.

my_list = ["Python", "Django", "Html", "Javascript", "Java"] # List

skip_index = 4 # 👉️ Index to skip

my_list.pop(skip_index) # 👉️ Remove by index

for i in my_list: # Loop Over new_list
    print(i)

Output:

Python
Django
Html
Javascript

If you want to skip the first item, You can also use the following example:

my_list = ["Python", "Django", "Html", "Javascript", "Java"] # List

new_list = my_list[1:] # 👉️ [1:] = index 1 to End

for i in new_list: # Loop Over new_list
    print(i)

Output:

Django
Html
Javascript
Java

Skip the last item:

my_list = ["Python", "Django", "Html", "Javascript", "Java"] # List

new_list = my_list[:-1] # 👉️ [:-1] = All items except the last one

for i in new_list: # Loop Over new_list
    print(i)

Output:

Python
Django
Html
Javascript

Conclusion

All right, In this article, we've coved skipping a value in a list. You can download all examples of this tutorial on GitHub.

If you want to learn more about Python data list, Scroll down, and you'll find the articles.

Happy Coding </>