Alex
Alex

Reputation: 51

How to split item in a list using python?

I have a list of items

mylist = ['sales.example.com', 'developer.example.com', 'example.com\ndistil.example.com\nnext.example.com\nstagepro.example.com\nwww.example.com', 'preferences.example.com']

I am trying to remove \n from each item. The result should look like this.

mylist = ['sales.example.com', 'developer.example.com', 'example.com', 'distil.example.com', 'next.example.com', 'stagepro.example.com', 'www.example.com', 'preferences.abc.com']

I have tried various methods including re.split(r'\n') and replace() but no luck.

Upvotes: 0

Views: 170

Answers (5)

Zeinab Mardi
Zeinab Mardi

Reputation: 131

I ask two questions:

  1. How to convert List to a String?
  2. How to convert String to a List?

list1 = list('sales.example.com developer.example.com example.com\ndistil.example.com\nnext.example.com\nstagepro.example.com\nwww.example.com preferences.example.com')

def listToString(s):  
    str1 = "" 
    for ele in s: 
        str1 += ele
    return str1

a = listToString(list2)
b = a.strip("\n")
def Convert(string): 
   li = list(string.split(" ")) 
   return li 

print(Convert(b)) 
['sales.example.com', 'developer.example.com',   'example.comdistil.example.comnext.example.comstagepro.example.comwww.example.com', 'preferences.example.com']

Upvotes: 0

Vishal Singh
Vishal Singh

Reputation: 6234

You can concatenate all the strings to form a single string using an \n separator and then use split() to split the string at \n.

'\n'.join(mylist).split('\n')

Upvotes: 1

Luc McCutcheon
Luc McCutcheon

Reputation: 109

My solution was to generate a new list by looping through each part given by splitting the list.

mylist = ['sales.example.com', 'developer.example.com', 
'example.com\ndistil.example.com\nnext.example.com\nstagepro.example.com\nwww.example.com', 'preferences.example.com']
newlist = []
for item in mylist:
    for web in item.split('\n'):
        newlist.append(web)

newlist: ['sales.example.com', 'developer.example.com', 'example.com', 'distil.example.com', 'next.example.com', 'stagepro.example.com', 'www.example.com', 'preferences.example.com']

Upvotes: 0

Krishna Chaurasia
Krishna Chaurasia

Reputation: 9600

You can use splitlines() combined with list comprehension:

mylist = [e for elt in mylist for e in elt.splitlines()]

Output:

['sales.example.com', 'developer.example.com', 'example.com', 'distil.example.com', 'next.example.com', 'stagepro.example.com', 'www.example.com', 'preferences.example.com']

Upvotes: 3

Alain T.
Alain T.

Reputation: 42139

You need to split every string in the list in a comprehension and use a nested level to get them back individually into a single list:

mylist = ['sales.example.com', 'developer.example.com', 'example.com\ndistil.example.com\nnext.example.com\nstagepro.example.com\nwww.example.com', 'preferences.example.com']

mylist = [ s for a in mylist for s in a.split("\n") ]

print(mylist)

['sales.example.com', 'developer.example.com', 'example.com',
 'distil.example.com', 'next.example.com', 'stagepro.example.com',
 'www.example.com', 'preferences.example.com']

Upvotes: 2

Related Questions