alvas
alvas

Reputation: 122052

Replace whitespaces with dashes for each item in a list -python

Is there a way of simplifying this loop where i replaces whitespace with dashes for each item in a list?

for item in a_list:
    alist[alist.index(item)] = '-'.join(item.split(" "))

or is this better?

for item in a_list:
    alist[alist.index(item)] = item.replace(" ", "-")

NOTE: The above solution only updates the 1st occurrence in this list, as David suggested, use list comprehension to do the above task.

I have a list of words and some have dashes while some doesn't. The items in a_list looks like this:

this-item has a-dash
this has dashes
this should-have-more dashes
this foo
doesnt bar
foo
bar

The output should look like this, where all items in list should have dashes instead of whitespace:

this-item-has-a-dash
this-has-dashes
this-should-have-more-dashes
this-foo
doesnt-bar
foo
bar

Upvotes: 4

Views: 3750

Answers (2)

abarnert
abarnert

Reputation: 365737

When you find yourself using the index method, you've probably done something wrong. (Not always, but often enough that you should think about it.)

In this case, you're iterating a list in order, and you want to know the index of the current element. Looking it up repeatedly is slow (it makes an O(N) algorithm O(N^3))—but, more importantly, it's fragile. For example, if you have two identical items, index will never find the second one.

This is exactly what enumerate was created for. So, do this:

for i, item in enumerate(a_list):
    alist[i] = '-'.join(item.split(" "))

Meanwhile, you could replace the loop with a list comprehension:

a_list = ['-'.join(item.split(" ")) for item in a_list]

This could be slower or use more memory (because you're copying the list rather than modifying it in-place), but that almost certainly doesn't matter (it certainly won't be as slow as your original code), and immutable algorithms are simpler and easier to reason about—and more flexible; you can call this version with a tuple, or an arbitrary iterable, not just a list.

As another improvement, do you really need to split and then join, or can you just use replace?

a_list = [item.replace(" ", "-") for item in a_list]

You could use regular expressions instead, which might be better for performance or readability in some similar cases—but I think in this case it would actually be worse. So, once you get here, you're done.

Upvotes: 2

David Robinson
David Robinson

Reputation: 78610

Use a list comprehension:

a_list = [e.replace(" ", "-") for e in a_list]

Upvotes: 8

Related Questions