Python List Slicing with map and lambda
This Snippet is coded by a user inOnline python Compiler. You can see and run this code too.
زبان: python
This Python code demonstrates how to extract a specific portion of each string in a list using the map function and a lambda expression. The list contains items like '4533-ali', where the first 5 characters (including the hyphen) are removed to isolate the name.
Steps:
- Define a list of strings.
- Set
n = 5as the number of characters to skip. - Use
map(lambda i: i[n:], mylist)to slice each string from index 5 onward. - Convert the result to a list and print it.
The output will be: ['ali', 'maryam', 'mohammad', 'mahsa']. This technique is useful for parsing structured text or cleaning data.