Let\'s say we have a list such as:
g = [\"123456789123456789123456\",
\"1234567894678945678978998879879898798797\",
\"654654656465656565656565565
Use a list comprehension:
[elem[:12] for elem in g]
Another option is to use map(...)
:
b = map(lambda x: x[:9],g)
Use a list comprehension:
g2 = [elem[:12] for elem in g]
If you prefer to edit g
in-place, use the slice assignment syntax with a generator expression:
g[:] = (elem[:12] for elem in g)
Demo:
>>> g = ['abc', 'defg', 'lolololol']
>>> g[:] = (elem[:2] for elem in g)
>>> g
['ab', 'de', 'lo']