0

I have a list of addresses. Some address have the street, city, state, zip and others just have city, state, and zip. I made a for loop to put each element into a separate variable. The problem is I am not getting the right output so I put an if statement with isinstance(address[3], int], I did this to check if the 4th element is there then execute the code but it's just not working. I'll post below to show what I am doing and let me know where I messed up. The first address has a street and the second doesn't, so I want to substitute the address for "-".

address = [['123 street name, New Orleans, LA, 12345'],['New Orleans, LA, 12345']]
if isinstance(address[3], int):
    street = address[0]
    city = address[1]
    city = city.lstrip()
    state = address[2]
    state = state.lstrip()
    zip_code = address[3]
else:
    street = "-"
    city = address[0]
    city = city.lstrip()
    state = address[1]
    state = state.lstrip()
    zip_code = address[2]
Kamikaze_goldfish
  • 856
  • 1
  • 10
  • 24

4 Answers4

1

You have the basic right idea. I think the biggest problem is with the condition in your if statement because address[3] will throw an exception when there are only 3 elements. Instead, you should check the length of the list with len(). If the list has length 3, then you only get the city, state, and zip into the correct variables. If the list has length 4, then you get all 4 fields.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • Yes that is the problem I was having. Actually it didn't throw an exception but somehow copied the previous and just duplicated it. The `len()` was the trick. – Kamikaze_goldfish Aug 22 '18 at 20:38
  • @Kamikaze_goldfish You should also consider how the data is obtained in the first place. If it is hardcoded, use a list of dictionaries, rather than a list of lists. – Code-Apprentice Aug 23 '18 at 14:25
1

The first line

address = [['123 street name, New Orleans, LA, 12345'],['New Orleans, LA, 12345']]

Actually has the affect of making a list of lists, where each inner list contains a single string. I think it might have a better affect if you write:

addresses = [['123 street name', 'New Orleans', 'LA', '12345'],['New Orleans', 'LA', '12345']]

To check if those fields are there, I would try

for address in addresses:
    if len(address) == 4:
        //case 1
    else:
        //case 2

If you don't want to take the trouble of breaking the string up the following line will convert your addresses:

addresses = [address.split(', ') for address in addresses]
Malcoto
  • 89
  • 6
1

There are multiple issues with your code.. first of all address is a list of lists length one. Second, if you'd like to check if the address contains a zip code, both of your examples do, but you can't access them separately from the other data since it is stored as a String right now. Try separating the elements and actually storing them as either Strings or ints. The next issue is that you can't check an element which does not exist. If you know the other data must be present, you can check the length of the list. In summary, use these changes:

addresses = [['123 street name', 'New Orleans', 'LA', 12345],['New Orleans', 'LA', 12345]]

for address in addresses:
    if len(address) > 3:
        street = address[0]
        city = address[1]
        city = city.lstrip()
        state = address[2]
        state = state.lstrip()
        zip_code = address[3]
    else:
        street = "-"
        city = address[0]
        city = city.lstrip()
        state = address[1]
        state = state.lstrip()
        zip_code = address[2]
J. Blackadar
  • 1,821
  • 1
  • 11
  • 18
0

First you need to split your addresses at ,, then check the length and assign the parts to values:

adress = '123 street name, New Orleans, LA, 12345'
parts = address.split(', ')
street, city, state, zip_code = parts if len(parts) == 4 else ['-'] + parts    
Daniel
  • 42,087
  • 4
  • 55
  • 81