Given a list, we always come across situations in which we require to apply certain function to each element in a list. This can be easily done by applying a loop and performing an operation to each element. But having shorthands to solve this problem is always beneficial and helps to focus more on important aspects of problem. Let’s discuss certain ways in which each element of list can be operated.
Method #1 : Using list comprehension
This method performs the same task in the background as done by the looping constructs. The advantage this particular method provides is that this is a one liner and also improves code readability.
The original list is : ['geeks', 'for', 'geeks', 'is', 'best'] The uppercased list is : ['GEEKS', 'FOR', 'GEEKS', 'IS', 'BEST']
Method #2 : Using
Using map function, one can easily associate an element with the operation one wishes to perform. This is the most elegant way to perform or solve this kind of problems.
The original list is : ['Geeks', 'foR', 'gEEks', 'IS', 'bEST'] The uppercased list is : ['geeks', 'for', 'geeks', 'is', 'best']
- Python | Replace elements in second list with index of same element in first list
- Python | Indices list of matching element from other list
- Python | Slicing list from Kth element to last element
- Python | Move one list element to another list
- Python | Every Kth element in list
- Python | Three element sum in list
- Python | How to get the last element of list
- Python | Last occurrence of some element in a list
- Python | Top N pairs by Kth element from list
- Python | Remove given element from the list
- Python | Element repetition in list
- Python | Equidistant element list
- Python | Operate on every Kth element of list
- Python | Insert after every Nth element in a list
- Python | Move element to end of the list
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.