- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Square list of elements in sorted form in Python
Suppose we have a sorted list of numbers; we have to square each element and find the output in sorted order. We can also put negative numbers and 0 as input.
So, if the input is like [-12,-6,-5,-2,0,1,2,4,8,9,10,15,18,20,35,38,69], then the output will be [0, 1, 4, 4, 16, 25, 36, 64, 81, 100, 144, 225, 324, 400, 1225, 1444, 4761]
To solve this, we will follow these steps −
- make a new list L
- for each element e in nums:
- insert e^2 into L
- return L in sorted order.
Let us see the following implementation to get better understanding −
Example
class Solution: def solve(self, nums): return sorted(x * x for x in nums) ob = Solution() nums = [1,2,4,8,9,10,15,18,20,35,38,69] print(ob.solve(nums))
Input
[-12,-6,-5,-2,0,1,2,4,8,9,10,15,18,20,35,38,69]
Output
[ 1, 4, 4, 16, 25, 36, 64, 81, 100, 144, 225, 324, 400, 1225, 1444, 4761]
- Related Articles
- Program to merge two sorted list to form larger sorted list in Python
- Program to find squared elements list in sorted order in Python
- Convert list of string into sorted list of integer in Python
- Square of a sorted array in C++
- List frequency of elements in Python
- How to generate a sorted list in Python?
- Python - Inserting item in sorted list maintaining order
- Check if the elements of stack are pairwise sorted in Python
- Python – Fractional Frequency of elements in List
- Check if list is sorted or not in Python
- Why doesn’t list.sort() return the sorted list in Python?
- Find missing numbers in a sorted list range in Python
- Delete List Elements in Python
- Python – Adjacent elements in List
- To print all elements in sorted order from row and column wise sorted matrix in Python

Advertisements