Question

In Python, How to return a copy of the list, return the max value in the...

In Python, How to return a copy of the list, return the max value in the list, flatten the list of lists to have only one level, e.g. >>> flatten([[1, 2], [3, 4], [5, 6]]) >>> [1, 2, 3, 4, 5, 6]

I want to solve the problem without a list comprehension and with a list comprehension to see the difference with comments to be able to understand it

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#list comprehension can only be used in flattening the list. There is no need to use it in
#finding maximum in list and creating the copy of list


#function which will flatten the list of list without a list comprehension
def flat(l):
#declare an empty list output
output=[]
#outer loop run over the list l passed
for i in l:
#inner loop run over each element of l in each iteration
for j in i:
#append the individual element into the output list
output.append(j)
#print the output flattened list
print("Flattened list without using list comprehension: ",output)

#function which will flatten the list by using list comprehension
def flat1(l1):
#declare an empty list output1
output1=[]
#list comprehension is basically reducing the space taken by the code
#which will generate same result as the previous code
#Now here nested loops are same as previous one except that it is written in same line
#loop will run and j is the individual element which will be appended one by one in output1
#list in each iteration
output1=[j for i in l1 for j in i ]
#printing the list output1
print("Flattened list by using list comprehension: ",output1)

#function which will return the copy of the list passed
#this copy will be shallow copy so that changing the copy of list
#does not change the original list l
def copy_list(l):
#using copy() method to create shallow copy of list l
return l.copy()
#function which will return the maximum number from the list
def max_list(l):
#setting m variable to first element of list
m=l[0]
#iterating over the list l
for i in l:
#if the element coming in iteration > the first element that is m then
#m is changed to that iterated element
if(i>m):
m=i
#Atlast m will be the maximum element so it will be returned
return m
#defining a listof lists l
l=[[1,2],[3,4],[5,6]]
#passing it to flat function
flat(l)
#passing it to flat1 function
flat1(l)
#defining another list l1
l1=[34,82,51,67]
#calling function which will return the copy of the list passed and storing it in l2
l2=copy_list(l1)
#printing l2
print("Copy of list: ",l2)
#using l2 copy of l1 to find maximum in it by calling the function max_list
#and passing l2 to it
#max number returned gets printed
print("Maximum element of list: ",max_list(l2))
  
#list comprehension can only be used in flattening the list. There is no need to use it in #finding maximum in list and creat#iterating over the list i for i in 1: #if the element coming in iteration > the first element that is m then #m is changed t6] Flattened list without using list comprehension: (1, 2, 3, 4, 5, Flattened list by using list comprehension: (1, 2, 3, 4,

Add a comment
Know the answer?
Add Answer to:
In Python, How to return a copy of the list, return the max value in the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT