3

d is a string list where each item is composed of two parts (connected by a dot). I want to extract the parts before and after the dot for each string item.

Here is how I did it.

d = ['a1.b1', 'a2.b2', 'a3.b3']
b = [c.split('.')[0] for c in d]
a = [c.split('.')[1] for c in d]

But I guess there is some more pythonic way?

Caffeinated
  • 11,982
  • 40
  • 122
  • 216
Lee
  • 526
  • 3
  • 9

1 Answers1

7

You can use zip() function:

>>> a,b = zip(*[i.split('.') for i in d])
>>> a
('a1', 'a2', 'a3')
>>> b
('b1', 'b2', 'b3')
Mazdak
  • 105,000
  • 18
  • 159
  • 188
  • 1
    You don't need to use a list comp `zip(*(i.split('.') for i in d))` – Padraic Cunningham Nov 11 '15 at 19:59
  • @PadraicCunningham I think using list comprehension has more performance than generator expression in terms of execution time. Because by using generator expression python will does an extra jobs again (calling the generator function and using `iter` and `next` methods ), ;-) . read my answer here http://stackoverflow.com/questions/33634130/why-is-any-running-slower-than-using-loops/33634284 – Mazdak Nov 11 '15 at 20:03
  • If you have large amounts of data you are potentially creating multiple copies which may cause issue – Padraic Cunningham Nov 11 '15 at 20:04
  • @PadraicCunningham Yep, in terms of memory use actually a generator expression would performs so much better. – Mazdak Nov 11 '15 at 20:05
  • Watch out, though; if `d` is empty, the assignment will fail due to not enough values to unpack! – user2357112 Nov 11 '15 at 20:09
  • @user2357112 Yes but it seems that `d` is not variable. – Mazdak Nov 11 '15 at 20:11