Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Issues using .join() and .pop() on one line

I just spent a lot of time trying to solve the following very low level coding challenge on Edabit.com:

This is a list of single characters with an unwanted character at the end:

["H", "e", "l", "l", "o", "!", "\0"]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You could also just type "Hello!" when initializing a variable,
creating the string "Hello!"

Create a function that will return a string by combining the given
character list, not including the unwanted final character. Examples

cpp_txt(["H", "i", "!", "\0"]) âžž "Hi!"

I couldn’t solve this trying the following one one line:

return "".join(lst.pop(-1))

or return "".join(lst.pop())

I get very strange results this way:

FAILED: '\x00' should equal 'Hi!'
ERROR: Traceback:
   in <module>
  File "./frameworks/python/cw-2.py", line 28, in assert_equals
    expect(actual == expected, message, allow_raise)
  File "./frameworks/python/cw-2.py", line 18, in expect
    raise AssertException(message)
cw-2.AssertException: '\x00' should equal 'Hi!'

I tried all kinds of things, like .strip(‘\x00’) but nothing worked. I used a for loop, I got results like ‘Hi!x00’.

Finally I gave up, and the solutions are either this:

def cpp_txt(lst):
return ''.join(lst[:-1])

that was probably what I should’ve thought of in the first place,
or this:

def cpp_txt(lst):
a = lst.pop(-1)
return "".join(lst)

Now, analyzing the second solution I guess I am using .pop() wrong, it seems like you need to create a separate variable to be able to ‘clean up’ the list, but this does not seem to be necessary in the following example I found in my browsing history on another website:

data_science_topics = ["Machine Learning", "SQL", "Pandas", "Algorithms", "Statistics", "Python 3"]
print(data_science_topics)

# Your code below: 
data_science_topics.pop()
print(data_science_topics)
data_science_topics.pop(3)
print(data_science_topics)

So, I hope someone would be so kind to explain me why the weird "\x00" appears in my results and how come I shouldn’t be using .join() and .pop() like this on a single line

Thank you for your time and patience.

>Solution :

When you do:

return "".join(lst.pop(-1))

It is equivalent of:

def cpp_txt(lst):
    a = lst.pop(-1)
    return "".join(a)  # NOTE: This is a ... not lst

In your working solution, you wanted to join the lst – i.e. return "".join(lst). But that is not what pop returns.

.pop() modifies the list provided to it – and returns just the last element.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading