python check if all list elements are strings

Python – Check If All Elements in List are Strings

Lists are a very versatile data structure in Python used to store ordered one-dimensional data and can store data of different types together. In this tutorial, we will look at how to check if all the elements in a list are strings or not in Python with the help of some examples.

How to check if all the list items are strings?

You can use the Python built-in isinstance() function to check whether an object is of a particular data type or not. For example, to check if a value is of str type or not, use isinstance(val, str).

But, the above function checks for only a single value. What if you want to know whether all the values in a list are of str type?

python check if all list elements are strings

Use the Python built-in all() function.

The all() function takes in an iterable as an argument and returns True if all the values in the iterable are truthy (represent True in a boolean context).

So, to check if all the values in a given list are of str type or not, use the all() function to check if each value in the list is of str type (using the isinstance() function). The following is the syntax –

# check if all the list values are of str type
all(isinstance(val, str) for val in ls)

It returns True if all the values in the list are of str type.

Examples

Let’s now look at some examples of using the above methods. First, we will create a few lists that we’ll use to demonstrate the methods.

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

# list with string values
ls1 = ['you', 'are', 'my', 'fire']
# list with int values
ls2 = [1, 2, 3, 4, 5]
# list with values of different types
ls3 = [14, 'cat', 12, True, 2.7]


# display the lists
print(ls1)
print(ls2)
print(ls3)

Output:

['you', 'are', 'my', 'fire']
[1, 2, 3, 4, 5]
[14, 'cat', 12, True, 2.7]

Here, we created three lists – ls1, ls2, and ls3. The list ls1 contains only string values as its elements, the list ls2 has only integer elements and the list ls3 contains elements of different types.

The idea here is to use the all() function to check if the type of each list element is str or not. You can use the Python built-in isinstance() function to check if a list value is of str type or not.

You can use a list comprehension to create a list of boolean values – whether a list element is a string or not and then pass this resulting list as an argument to the all() function.

Let’s apply this to the lists created above.

# check if all list values are strings
print(all([isinstance(val, str) for val in ls1]))
print(all([isinstance(val, str) for val in ls2]))
print(all([isinstance(val, str) for val in ls3]))

Output:

True
False
False

We get True for ls1 as it contains only string values and False for both ls2 and ls3 as all the values in these respective lists are not strings.

Note that the all() function takes an iterable as an argument, so you can directly use an iterator (without using a list comprehension).

# check if all list values are strings
print(all(isinstance(val, str) for val in ls1))
print(all(isinstance(val, str) for val in ls2))
print(all(isinstance(val, str) for val in ls3))

Output:

True
False
False

We get the same results as above.

Let’s see what happens if we apply the above method to an empty list.

# an empty list
ls4 = []

# check if all list values are of strings
print(all(isinstance(val, str) for val in ls4))

Output:

True

We get True as the output.

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top