Austin Reed
Austin Reed

Reputation: 37

How to check for a empty element within a list of elements in python

Say for example i have a list of lists that contain data like this:

    customer1 = ['Dan','24','red']
    customer2 = ['Bob',' ','Blue']
    customerlist = [customer1, customer2]

I would like to run a line of code that will run a function if one of these elements is empty. For example something like this:

    for c in customerlist:
        if not in c:
            ***RUN CODE***
        else:
            print('Customer Complete')

That way if a customer is missing data i can run some code.

Thanks for the help!

Upvotes: 0

Views: 90

Answers (4)

Bipin
Bipin

Reputation: 483

You may use Python Regular Expression to search for blank entries on the list. A Regular Expression is a sequence of characters that define a pattern. For more information on Python Regular Expression, kindly visit: w3school link and Google Developer link

Kindly replace the following code

for c in customerlist:
        if not in c:

with the following code:

for i in range(len(customerlist)):
    for j in range(len(customer1)):
        emptylist = re.findall('\s*', customerlist[i][j])

Dont forget to include 'import re' at the beginning of code to import Python re module

The complete code:

import re
customer1 = ['Dan','24','red']
customer2 = ['Bob',' ','Blue', ' ']
customerlist = [customer1, customer2]

for i in range(len(customerlist)):
    for j in range(len(customer1)):
        emptylist = re.findall('\s*', customerlist[i][j])
if(len(emptylist) == 0):
    print('There are no blank entries')
else:
    print('There are blank entries')
    #code goes here to do something

The output:

There are blank entries

In the code:

emptylist = re.findall('\s*', customerlist[i][j])

re.findall() search for zero or more instances(*) of white space character(\s) with customerlist being the iterating list. customerlist[i][j] as it is a list of lists.

Upvotes: 0

felipe
felipe

Reputation: 8025

Both of the answers given by Guy and John are correct, but perhaps it would interest you to look into objects:

class Customer:
    def __init__(self, name, age = None, color = None):
        self.name = name
        self.age = age if age else age_function_generator()
        self.color = color if color else color_function_generator()

To create a customer, then, simply do:

c1 = Customer(name = "Dan", age = 24, color = "red")
c2 = Customer(name = "Bob", color = "Blue")

In the case of c2 the function age_function_generator() (not defined here) would be called. To access the attributes of the customer object one would do:

print(c1.name, c1.age, c1.color)

Upvotes: 0

John Zwinck
John Zwinck

Reputation: 249093

Instead of this:

    if not in c:

You want this:

    for val in c:
        if not val.strip():

Which basically checks if any of the strings is empty (empty strings are "falsey" in Python). Stripping first detects strings which only contain whitespace.

Upvotes: 1

Guy
Guy

Reputation: 50809

You can use in to check for ' '

for c in customerlist:
    if ' ' in c:
        RUN CODE
    else:
        print('Customer Complete')

Upvotes: 1

Related Questions