0

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!

1
  • What is an empty element? Commented Dec 15, 2019 at 6:13

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

You can use in to check for ' '

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

5 Comments

This considers 'Bob ' to be an empty string.
@AlexanderCécile: I don't think so, you're looking at the wrong layer of the onion.
@AlexanderCécile no, it doesn't. c is a list, not string.
To expand on the above, you may also do: if any(x in a for x in (' ', '', 'empty', None)): to check for multiple representations of an empty index.
@Guy Right, I was looking at the wrong list, sorry.
0

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) 

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.