0

I'm new to Python, so I was hoping somebody could help me out with the following issue.

project_tags = browser.find_elements_by_xpath('//a[contains(@class, "campaignTags-tag")]')
separator = ', '
project_tags = separator.join(project_tags)

I know that if project_tags were an array, the join method would work. Hence I thought there may've been a chance the code above would've worked since project_tags is an iterable, but of course it didn't since it's an iterable of FirefoxWebElements.

I believe all I need to do in order to get join to work in this context is to point to the text of each FirefoxWebElement. Even though I can accomplish the aim in a couple other ways, I was just curious if this is possible?

One of those ways, which seems kind of verbose, is:

project_tags = browser.find_elements_by_xpath('//a[contains(@class, "campaignTags-tag")]')
for idx, tag in enumerate(project_tags):
    if idx is 0:
        tags_string = tag.text
    else:
        tags_string = tags_string + ', ' + tag.text if idx < len(project_tags) else tags_string + tag.text

Are there any methods I might not know about that would allow me to refactor and minimize the working example above?

oldboy
  • 5,729
  • 6
  • 38
  • 86

1 Answers1

0

I believe this does what you want:

tags_string = ", ".join([t.text for t in project_tags])

Note, however, that it includes a comma between the last 2 tags, which your code does not, which I believe is a bug. If not, you could use:

tags_string = ", ".join([t.text for t in project_tags[:-1]]) + project_tags[-1].text
Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
  • i realized it was a bug, but i'm so confused why `idx + 1 < len(project_tags)` doesn't work properly? for instance, if you had a list of 3 items, the first iteration triggers the first clause of the if statement since `idx` is `0`; for the second iteration, because `idx + 1` is `2` and `len(list)` is `3`, the first half of the "ternary" operator should be triggered, but instead it resolves to the `else` of the "ternary" operator??? why – oldboy Jul 19 '18 at 18:38
  • For the last iteration, `idx` = `len(project_tags)-1`, which means `idx+1` = `len(project_tags)`, and thus `idx+1 < len(project_tags)` is false. – Scott Hunter Jul 19 '18 at 20:15
  • precisely... that's what i would expect to happen, but for some reason `idx+1 < len(project_tags)` was resolving to false for the last 2 iterations, so that you would end up with output like this: `rock, hip-hoprap` or `oranges, bananasapples` – oldboy Jul 20 '18 at 01:48
  • That is the output you get if the expression is false in only the last iteration, which is in fact what was happening. – Scott Hunter Jul 20 '18 at 02:07
  • huh? there's no comma between `bananas` and `oranges` or `hip-hop` and `rap` because `idx+1 < len(project_tags)` is necessarily resolving to false for the last two inputs of each concatenation process. that's what's confusing me: why does it resolve to false for the last two inputs instead of just the very last input? – oldboy Jul 20 '18 at 03:32