For example:

lst1 = ['wer12', 'rtgdf12', 'werfd12'] 

How to make it happen:

 lst1 = ['wer','rtgdf','werfd'] 

    3 answers 3

    That's through generators:

     lst1 = ['wer12', 'rtgdf12', 'werfd12'] lst1 = [str[:-2] for str in lst1] print(lst1) 
    • This is a list comprehension (not sure if there is a Russian translation). The generator is round brackets. - Arnial
    • There are generators of lists, dictionaries and sets. Proof: pep8.ru/doc/dive-into-python-3/5.html - Dmitry Erohin
    • Looks like a translation problem. As a generator in python, I usually understand this , and this is how list-comprehension . I did not know that we also call it a generator. - Arnial 5:41
    • Perhaps thanks for the link, I will read. - Dmitry Erohin
    • This is a list inclusion - nick_gabpe

    The map function bypasses all elements of the list and cuts all characters to the last two of each line:

     res = map(lambda x: x[:-2], lst1) print(list(res)) 

      You can use slices to get part of a string.

       lst1 = list( map( lambda x : x[:-2], lst1 ) ) 

      lambda x: x [: - 2] is an anonymous function that returns a string without the last 2 characters.

      map --- applies the function to each element of the list, returns an iterator on the results of the function

      list --- translates an iterator into a list

      UPDATE:

      lambda is a python constructor for creating anonymous functions

       # такая запись lambda x: x[:-2] # аналогична такой, def f(x): return x[:-2] # с той лишь разницей, что не будет занято имя `f` 
      • If you can, please explain what 'lambda' is - Bernard
      • I think it is better to note the answer DmitryErohin as true. Using list-comprehension is a more pythonic way. Python documentation bluntly says this is more “concise and understandable . - Arnial 5:46