You need to use list.extend instead of list.append.
newlist = []
for word in lines:
word = word.split(",")
newlist.extend(word) # <----
return newlist
Or, using list comprehension:
>>> lst = ['60,78', '70,77', '80,74', '90,75', '100,74', '110,75']
>>> [x for xs in lst for x in xs.split(',')]
['60', '78', '70', '77', '80', '74', '90', '75', '100', '74', '110', '75']
Answer from falsetru on Stack OverflowYou need to use list.extend instead of list.append.
newlist = []
for word in lines:
word = word.split(",")
newlist.extend(word) # <----
return newlist
Or, using list comprehension:
>>> lst = ['60,78', '70,77', '80,74', '90,75', '100,74', '110,75']
>>> [x for xs in lst for x in xs.split(',')]
['60', '78', '70', '77', '80', '74', '90', '75', '100', '74', '110', '75']
str.split actually returns a list.
Return a list of the words in the string, using sep as the delimiter string.
Since you are appending the returned list to newlist, you are getting a list of lists. Instead use list.extend method, like this
for word in lines:
newlist.extend(word.split(","))
But you can simply use nested list comprehension like this
>>> data = ['60,78', '70,77', '80,74', '90,75', '100,74', '110,75']
>>> [item for items in data for item in items.split(",")]
['60', '78', '70', '77', '80', '74', '90', '75', '100', '74', '110', '75']
list - How to split by comma and strip white spaces in Python? - Stack Overflow
How to split a python list by comma - Stack Overflow
split element inside list by comma with python - Stack Overflow
regex - How can I split this comma-delimited string in Python? - Stack Overflow
Use list comprehension -- simpler, and just as easy to read as a for loop.
my_string = "blah, lots , of , spaces, here "
result = [x.strip() for x in my_string.split(',')]
# result is ["blah", "lots", "of", "spaces", "here"]
See: Python docs on List Comprehension
A good 2 second explanation of list comprehension.
I came to add:
map(str.strip, string.split(','))
but saw it had already been mentioned by Jason Orendorff in a comment.
Reading Glenn Maynard's comment on the same answer suggesting list comprehensions over map I started to wonder why. I assumed he meant for performance reasons, but of course he might have meant for stylistic reasons, or something else (Glenn?).
So a quick (possibly flawed?) test on my box (Python 2.6.5 on Ubuntu 10.04) applying the three methods in a loop revealed:
$ time ./list_comprehension.py # [word.strip() for word in string.split(',')]
real 0m22.876s
$ time ./map_with_lambda.py # map(lambda s: s.strip(), string.split(','))
real 0m25.736s
$ time ./map_with_str.strip.py # map(str.strip, string.split(','))
real 0m19.428s
making map(str.strip, string.split(',')) the winner, although it seems they are all in the same ballpark.
Certainly though map (with or without a lambda) should not necessarily be ruled out for performance reasons, and for me it is at least as clear as a list comprehension.
Using List Comprehension:
>>> industries_list = ["Computers, Internet","Photography, Tourism","Motoring, Manufacturing"]
>>> [s.split(',') for s in industries_list]
[['Computers', ' Internet'], ['Photography', ' Tourism'], ['Motoring', ' Manufacturing']]
And to remove the white-space:
>>> from string import strip
>>> [map(strip, s.split(',')) for s in industries_list]
[['Computers', 'Internet'], ['Photography', 'Tourism'], ['Motoring', 'Manufacturing']]
You could also use pure list-comprehension (embedded list comprehension):
>>> [[w.strip() for w in s.split(',')] for s in industries_list]
[['Computers', 'Internet'], ['Photography', 'Tourism'], ['Motoring', 'Manufacturing']]
Use .split on the string class:
>>> industries_list=["Computers, Internet","Photography, Tourism","Motoring, Manufacturing"]
>>> [var.split(',') for var in industries_list]
[['Computers', ' Internet'], ['Photography', ' Tourism'], ['Motoring', ' Manufacturing']]
If you'd like no spaces:
>>> [[s.strip() for s in var.split(',')] for var in industries_list]
[['Computers', 'Internet'], ['Photography', 'Tourism'], ['Motoring', 'Manufacturing']]
Live demo.
You don't want regular expressions here.
s = "144,1231693144,26959535291011309493156476344723991336010898738574164086137773096960,26959535291011309493156476344723991336010898738574164086137773096960,1.00,4295032833,1563,2747941 288,1231823695,26959535291011309493156476344723991336010898738574164086137773096960,26959535291011309493156476344723991336010898738574164086137773096960,1.00,4295032833,909,4725008"
print s.split(',')
Gives you:
['144', '1231693144', '26959535291011309493156476344723991336010898738574164086137773096960', '26959535291011309493156476344723991336010898738574164086137773096960', '1.00
', '4295032833', '1563', '2747941 288', '1231823695', '26959535291011309493156476344723991336010898738574164086137773096960', '26959535291011309493156476344723991336010898
738574164086137773096960', '1.00', '4295032833', '909', '4725008']
How about a list?
mystring.split(",")
It might help if you could explain what kind of info we are looking at. Maybe some background info also?
EDIT:
I had a thought you might want the info in groups of two?
then try:
re.split(r"\d*,\d*", mystring)
and also if you want them into tuples
[(pair[0], pair[1]) for match in re.split(r"\d*,\d*", mystring) for pair in match.split(",")]
in a more readable form:
mylist = []
for match in re.split(r"\d*,\d*", mystring):
for pair in match.split(",")
mylist.append((pair[0], pair[1]))