mos
mos

Reputation: 121

How do I split a name in a list then reinsert the split items?

I have lists inside the csv list:

newlist = [
    ['id', 'name', 'lastContactedTime', 'email', 'phone_phones', 'home_phones', 'mobile_phones', 'work_phones', 'fax_phones', 'other_phones', 'address_1', 'address_2', 'address_3', 'city', 'state', 'postal_code', 'country', 'tags'], 
    ['12-contacts', 'Courtney James', '', '[email protected]', '+1 3455463849', '', '', '', '', '', '654 Rodney Franklin street', '', '', 'Birmingham', 'AL', '45678', 'US', ''], 
    ['4-contacts', 'Joe Malcoun', '2019-08-13 14:41:12', '[email protected]', '', '', '', '', '', '', '212 South Fifth Ave', '', '', 'Ann Arbor', 'MI', '48103', 'US', ''], 
    ['8-contacts', 'Rafael Acosta', '', '[email protected]', '+1 338551534', '', '', '', '', '', '13 Jordan Avenue SW', '', '', 'Birmingham', 'AL', '45302', 'US', '']
]

I want to create a recurring event where I split the names like: "Courtney James" in each list and add it to a new list.

I have tried to split and append each name separately using a while loop to a list but it did not work out

#Splitting an item in the list and adding it to a new list  
m = 1
while newlist[m][1] != None:
    splitter = newlist[m][1].split()
    namelist = splitter
    m+1

    print(namelist)
else:
    break

I get errors or the code does not compile. I expect the names to be split and added to a new list.

My desired output would be recurring lists to be able to add it to a new excel worksheet using xlsxwriter: Headers= ['Lastname','First name','Company','Title','Willing To share', 'Willing to introduce', 'Work phone', 'Work email', 'Work street', 'Work City', ' Work State', 'Work Zip', 'Personal Street', 'Personal City', 'Personal State', 'Personal Zip', 'Mobile Phone', 'Personal email', 'Note', 'Note Category'] List1= ['Doe1', 'John', 'company1', 'CIO', 'Yes', 'Yes', '999-999-999', '[email protected]', '123 work street', 'workville', 'IL', '12345', '1234 personal street', 'peronville', 'Il', '12345', '999-999-999', '[email protected]', 'public note visible to everyone', 'Public'] List2= List3=

Upvotes: 0

Views: 65

Answers (1)

Booboo
Booboo

Reputation: 44313

When you split each name, you get a list such as [first_name, last_name]. Assuming you wanted to build up a "list of these lists", then you want to do the following using your code as a basis:

namelist = [] # new, empty list
for i in range(1, len(newlist)):
    names = newlist[i][1].split() # this yields [first_name, last_name]
    #print(names)
    namelist.append([names[1], names[0]]) # [last_name, first_name]
  • range(1, len(newlist)) generates the numbers 1, 2, ... length of newlist - 1
  • namelist.append([names[1], names[0]]) appends the split names to our new list

The result:

[['James', 'Courtney'], ['Malcoun', 'Joe'], ['Acosta', 'Rafael']]

What you are looking for is a more complicated list with other elements in it. But at least the above code shows how to properly loop through your original list.

Upvotes: 0

Related Questions