I have a list sample_data = [1,2,3,4,5,6,7,8,9,10], and I want [1,4,9,16,25,36,49,64,81,100] from it. What should be the possible solution?
Question
I have a list
sample_data = [1,2,3,4,5,6,7,8,9,10], and I want
[1,4,9,16,25,36,49,64,81,100] from it.
What should be the possible solution?
Solution
You can achieve this by using list comprehension in Python. Here's how you can do it:
sample_data = [1,2,3,4,5,6,7,8,9,10]
squared_data = [i**2 for i in sample_data]
print(squared_data)
In this code, i**2
squares each item in the list sample_data
. The square of each item is then added to the new list squared_data
. The print(squared_data)
statement will output your desired list [1,4,9,16,25,36,49,64,81,100]
.
Similar Questions
Given list is [1,2,3,"d",4,5,"a"] separate the elements based on their data types
Given list is [1,2,3,4,"a","b"] append each elements datatypes to separate lists.
Write a Python program to accept a list of 10 integers and find the frequency ofany given data item (Without using predefined function
Given listA = [1,2,3,4,5,5,6,6,7,7,7,8,8,8,8] What will be the output of print(set(listA))?
Given a 2D list, create a numpy 2D array using it.Note: Given 2D list is [[1, 2, 3], [4, 5, 6], [7, 8, 9]]Print the Numpy array.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.