- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Program to update list items by their absolute values in Python

Suppose we have a list of numbers called nums with positive and negative numbers. We have to update this list so that the final list will only hold the absolute value of each element.

So, if the input is like nums = [5,-7,-6,4,6,-9,3,-6,-2], then the output will be [5, 7, 6, 4, 6, 9, 3, 6, 2]

To solve this, we will follow these steps −

- Solve this by map and list operations
- define one anonymous function say l, that takes x as argument and returns abs(x)
- using map() method convert each element e from nums to l(e)
- return the list

## Example

Let us see the following implementation to get better understanding −

def solve(nums): return list(map(lambda x:abs(x), nums)) nums = [5,-7,-6,4,6,-9,3,-6,-2] print(solve(nums))

## Input

[5,-7,-6,4,6,-9,3,-6,-2]

## Output

[5, 7, 6, 4, 6, 9, 3, 6, 2]

- Related Questions & Answers
- Python program to sort tuples by frequency of their absolute difference
- Python program to Sort a List of Dictionaries by the Sum of their Values
- Python – Sort List items on basis of their Digits
- Python – Sort List items on the basis of their Digits
- Python program to Sort Tuples by their Maximum element
- How to replace auto-labelled relative values by absolute values in Matplotlib?
- Fetching JavaScript keys by their values - JavaScript
- Python - Create nested list containing values as the count of list items
- How to remove list elements by their name in R?
- Program to reverse a list by list slicing in Python
- Ways to sort list of dictionaries by values in Python
- Program to split a list of numbers such that the absolute difference of median values are smallest in Python
- Program to count average of all special values for all permutations of a list of items in Python
- How to update a Python dictionary values?
- Python program to find sum of absolute difference between all pairs in a list

Advertisements