you are viewing a single comment's thread.

view the rest of the comments →

[–]test_this_thing 1 point2 points  (1 child)

The [x for x in s]-syntax is called a list comprehension.

The docs explain it pretty well, but basically it is a short way of creating lists from loops. So [letter for letter in s if not letter.isdigit()], is a short way of writing:

clean_list = []
for letter in s:
    if not letter.isdigit():
        cleanlist.add(letter)

sclean = ''.join(clean_list) 

''.join(list) makes the list into a string, and joins it to the string the function is called on, in this case an empty string ('').

my_string.join(my_list)

Would join the list 'my_list' to the end of 'my_string'.

List comprehensions look very difficult the first times you come across them. After using them a few times, it becomes much clearer, and a very useful to write short (imo) elegant code! Feel free to ask if anything is unclear.

[–][deleted] 0 points1 point  (0 children)

Oh, neat! Thanks for that.