SlideShare a Scribd company logo
1 of 1
Download to read offline
Lists can contain other lists as elements. For example the list
HAIRY=[1,2,[3,4,[5,[6,7.8],[9,10]],11,[12,[13,14],[15,16]]]] contains several lists containing
other lists -- ie it is a "multilevel" list. Write a function flatten(L) which takes a possibly multi-
level list L as an argument and returns a new list with the same elements in the same overall
order as L but with only one level. For example, flatten(HAIRY) would return the list
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
Solution
Please find the required program along with its output. Please see the comments against each line
to understand the step.
def flatten(L): #recursive function to flatten a list
for element in L: #iterate through each element of a list
if not isinstance(element, list): # if the element is not a list,
yield element # just return the element
else: #if the element is a list, then recursivley call the flatten fucntion on the sub list, and return
each element individually
for x in flatten(element):
yield x
print "Flattened list elements are: "
for x in flatten([1,2,[3,4,[5,[6,7,8],[9,10]],11,[12,[13,14],[15,16]]]]):
print x, #print the flatten list elements
----------------------------------------
OUTPUT :
Flattened list elements
are:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

More Related Content

Similar to Lists can contain other lists as elements. For example the list HAIR.pdf

Provide the implementations and signatures for the following operati.pdf
Provide the implementations and signatures for the following operati.pdfProvide the implementations and signatures for the following operati.pdf
Provide the implementations and signatures for the following operati.pdfalsofshionchennai
 
Data structures: linear lists
Data structures: linear listsData structures: linear lists
Data structures: linear listsToniyaP1
 
List Data Structure.docx
List Data Structure.docxList Data Structure.docx
List Data Structure.docxmanohar25689
 
Functional programming seminar (haskell)
Functional programming seminar (haskell)Functional programming seminar (haskell)
Functional programming seminar (haskell)Bikram Thapa
 
Python Unit 5 Questions n Notes.pdf
Python Unit 5 Questions n Notes.pdfPython Unit 5 Questions n Notes.pdf
Python Unit 5 Questions n Notes.pdfMCCMOTOR
 
Fp in scala part 1
Fp in scala part 1Fp in scala part 1
Fp in scala part 1Hang Zhao
 
Java AssignmentUsing the ListNode.java file below Write method.pdf
Java AssignmentUsing the ListNode.java file below Write method.pdfJava AssignmentUsing the ListNode.java file below Write method.pdf
Java AssignmentUsing the ListNode.java file below Write method.pdfambersushil
 
The following class stores a list of integer values and contains a met.docx
The following class stores a list of integer values and contains a met.docxThe following class stores a list of integer values and contains a met.docx
The following class stores a list of integer values and contains a met.docxcarold11
 
11 Introduction to lists.pptx
11 Introduction to lists.pptx11 Introduction to lists.pptx
11 Introduction to lists.pptxssuser8e50d8
 
Write a method expand that could be added to the LinkedlntList class f.docx
Write a method expand that could be added to the LinkedlntList class f.docxWrite a method expand that could be added to the LinkedlntList class f.docx
Write a method expand that could be added to the LinkedlntList class f.docxnoreendchesterton753
 
Write a method expand that could be added to the LinkedlntList class .docx
 Write a method expand that could be added to the LinkedlntList class .docx Write a method expand that could be added to the LinkedlntList class .docx
Write a method expand that could be added to the LinkedlntList class .docxajoy21
 
C++ help! Write a function merge that merges two lists into one- alter.docx
C++ help! Write a function merge that merges two lists into one- alter.docxC++ help! Write a function merge that merges two lists into one- alter.docx
C++ help! Write a function merge that merges two lists into one- alter.docxgilliandunce53776
 
The Ring programming language version 1.6 book - Part 24 of 189
The Ring programming language version 1.6 book - Part 24 of 189The Ring programming language version 1.6 book - Part 24 of 189
The Ring programming language version 1.6 book - Part 24 of 189Mahmoud Samir Fayed
 

Similar to Lists can contain other lists as elements. For example the list HAIR.pdf (20)

Provide the implementations and signatures for the following operati.pdf
Provide the implementations and signatures for the following operati.pdfProvide the implementations and signatures for the following operati.pdf
Provide the implementations and signatures for the following operati.pdf
 
Data structures: linear lists
Data structures: linear listsData structures: linear lists
Data structures: linear lists
 
haskell_fp1
haskell_fp1haskell_fp1
haskell_fp1
 
List Data Structure.docx
List Data Structure.docxList Data Structure.docx
List Data Structure.docx
 
Functional programming seminar (haskell)
Functional programming seminar (haskell)Functional programming seminar (haskell)
Functional programming seminar (haskell)
 
Python Unit 5 Questions n Notes.pdf
Python Unit 5 Questions n Notes.pdfPython Unit 5 Questions n Notes.pdf
Python Unit 5 Questions n Notes.pdf
 
Fp in scala part 1
Fp in scala part 1Fp in scala part 1
Fp in scala part 1
 
Ch-8.pdf
Ch-8.pdfCh-8.pdf
Ch-8.pdf
 
Java AssignmentUsing the ListNode.java file below Write method.pdf
Java AssignmentUsing the ListNode.java file below Write method.pdfJava AssignmentUsing the ListNode.java file below Write method.pdf
Java AssignmentUsing the ListNode.java file below Write method.pdf
 
Groovy
GroovyGroovy
Groovy
 
The following class stores a list of integer values and contains a met.docx
The following class stores a list of integer values and contains a met.docxThe following class stores a list of integer values and contains a met.docx
The following class stores a list of integer values and contains a met.docx
 
11 Introduction to lists.pptx
11 Introduction to lists.pptx11 Introduction to lists.pptx
11 Introduction to lists.pptx
 
FINAL revised LIST in Python.pdf
FINAL revised LIST in Python.pdfFINAL revised LIST in Python.pdf
FINAL revised LIST in Python.pdf
 
Lists_tuples.pptx
Lists_tuples.pptxLists_tuples.pptx
Lists_tuples.pptx
 
Write a method expand that could be added to the LinkedlntList class f.docx
Write a method expand that could be added to the LinkedlntList class f.docxWrite a method expand that could be added to the LinkedlntList class f.docx
Write a method expand that could be added to the LinkedlntList class f.docx
 
Write a method expand that could be added to the LinkedlntList class .docx
 Write a method expand that could be added to the LinkedlntList class .docx Write a method expand that could be added to the LinkedlntList class .docx
Write a method expand that could be added to the LinkedlntList class .docx
 
C++ help! Write a function merge that merges two lists into one- alter.docx
C++ help! Write a function merge that merges two lists into one- alter.docxC++ help! Write a function merge that merges two lists into one- alter.docx
C++ help! Write a function merge that merges two lists into one- alter.docx
 
General Data structures
General Data structuresGeneral Data structures
General Data structures
 
The Ring programming language version 1.6 book - Part 24 of 189
The Ring programming language version 1.6 book - Part 24 of 189The Ring programming language version 1.6 book - Part 24 of 189
The Ring programming language version 1.6 book - Part 24 of 189
 
Lecture 09.pptx
Lecture 09.pptxLecture 09.pptx
Lecture 09.pptx
 

More from fasttracksunglass

I have been working on this ROCK, PAPER, SCISSORS project for the pa.pdf
I have been working on this ROCK, PAPER, SCISSORS project for the pa.pdfI have been working on this ROCK, PAPER, SCISSORS project for the pa.pdf
I have been working on this ROCK, PAPER, SCISSORS project for the pa.pdffasttracksunglass
 
Describe tolerance as a immunologic function. What is the consequenc.pdf
Describe tolerance as a immunologic function. What is the consequenc.pdfDescribe tolerance as a immunologic function. What is the consequenc.pdf
Describe tolerance as a immunologic function. What is the consequenc.pdffasttracksunglass
 
Answer the following question about human evolution as inferred from.pdf
Answer the following question about human evolution as inferred from.pdfAnswer the following question about human evolution as inferred from.pdf
Answer the following question about human evolution as inferred from.pdffasttracksunglass
 
40.Classification of a network as a LAN or a WAN is not relevant to .pdf
40.Classification of a network as a LAN or a WAN is not relevant to .pdf40.Classification of a network as a LAN or a WAN is not relevant to .pdf
40.Classification of a network as a LAN or a WAN is not relevant to .pdffasttracksunglass
 
12.9 Program Online shopping cart (continued) (C++)This program e.pdf
12.9 Program Online shopping cart (continued) (C++)This program e.pdf12.9 Program Online shopping cart (continued) (C++)This program e.pdf
12.9 Program Online shopping cart (continued) (C++)This program e.pdffasttracksunglass
 
A sterile item is free ofmicrobes.endospores.viruses.prions..pdf
A sterile item is free ofmicrobes.endospores.viruses.prions..pdfA sterile item is free ofmicrobes.endospores.viruses.prions..pdf
A sterile item is free ofmicrobes.endospores.viruses.prions..pdffasttracksunglass
 
Both mitochondria and chloroplasts... A. obtain electrons from water .pdf
Both mitochondria and chloroplasts... A. obtain electrons from water .pdfBoth mitochondria and chloroplasts... A. obtain electrons from water .pdf
Both mitochondria and chloroplasts... A. obtain electrons from water .pdffasttracksunglass
 
Please answer all parts thank you!Parents PrPr YY rr x   PrPr.pdf
Please answer all parts thank you!Parents PrPr YY rr x   PrPr.pdfPlease answer all parts thank you!Parents PrPr YY rr x   PrPr.pdf
Please answer all parts thank you!Parents PrPr YY rr x   PrPr.pdffasttracksunglass
 
Low platelet count it a recessively inherited trait. Reevaluation of .pdf
Low platelet count it a recessively inherited trait. Reevaluation of .pdfLow platelet count it a recessively inherited trait. Reevaluation of .pdf
Low platelet count it a recessively inherited trait. Reevaluation of .pdffasttracksunglass
 
Match the modified stem to its correct definition.StolonRhizome.pdf
Match the modified stem to its correct definition.StolonRhizome.pdfMatch the modified stem to its correct definition.StolonRhizome.pdf
Match the modified stem to its correct definition.StolonRhizome.pdffasttracksunglass
 
JAVAAdd to the code at the bottom to do the following two things.pdf
JAVAAdd to the code at the bottom to do the following two things.pdfJAVAAdd to the code at the bottom to do the following two things.pdf
JAVAAdd to the code at the bottom to do the following two things.pdffasttracksunglass
 
Let (X,T) be a topological space,.A subset X. Suppose that for all x .pdf
Let (X,T) be a topological space,.A subset X. Suppose that for all x .pdfLet (X,T) be a topological space,.A subset X. Suppose that for all x .pdf
Let (X,T) be a topological space,.A subset X. Suppose that for all x .pdffasttracksunglass
 
Choose the best possible answer for each of the following multiple ch.pdf
Choose the best possible answer for each of the following multiple ch.pdfChoose the best possible answer for each of the following multiple ch.pdf
Choose the best possible answer for each of the following multiple ch.pdffasttracksunglass
 
If T is a bounded and self-adjoint operator on a Hilbert space and T_.pdf
If T is a bounded and self-adjoint operator on a Hilbert space and T_.pdfIf T is a bounded and self-adjoint operator on a Hilbert space and T_.pdf
If T is a bounded and self-adjoint operator on a Hilbert space and T_.pdffasttracksunglass
 

More from fasttracksunglass (14)

I have been working on this ROCK, PAPER, SCISSORS project for the pa.pdf
I have been working on this ROCK, PAPER, SCISSORS project for the pa.pdfI have been working on this ROCK, PAPER, SCISSORS project for the pa.pdf
I have been working on this ROCK, PAPER, SCISSORS project for the pa.pdf
 
Describe tolerance as a immunologic function. What is the consequenc.pdf
Describe tolerance as a immunologic function. What is the consequenc.pdfDescribe tolerance as a immunologic function. What is the consequenc.pdf
Describe tolerance as a immunologic function. What is the consequenc.pdf
 
Answer the following question about human evolution as inferred from.pdf
Answer the following question about human evolution as inferred from.pdfAnswer the following question about human evolution as inferred from.pdf
Answer the following question about human evolution as inferred from.pdf
 
40.Classification of a network as a LAN or a WAN is not relevant to .pdf
40.Classification of a network as a LAN or a WAN is not relevant to .pdf40.Classification of a network as a LAN or a WAN is not relevant to .pdf
40.Classification of a network as a LAN or a WAN is not relevant to .pdf
 
12.9 Program Online shopping cart (continued) (C++)This program e.pdf
12.9 Program Online shopping cart (continued) (C++)This program e.pdf12.9 Program Online shopping cart (continued) (C++)This program e.pdf
12.9 Program Online shopping cart (continued) (C++)This program e.pdf
 
A sterile item is free ofmicrobes.endospores.viruses.prions..pdf
A sterile item is free ofmicrobes.endospores.viruses.prions..pdfA sterile item is free ofmicrobes.endospores.viruses.prions..pdf
A sterile item is free ofmicrobes.endospores.viruses.prions..pdf
 
Both mitochondria and chloroplasts... A. obtain electrons from water .pdf
Both mitochondria and chloroplasts... A. obtain electrons from water .pdfBoth mitochondria and chloroplasts... A. obtain electrons from water .pdf
Both mitochondria and chloroplasts... A. obtain electrons from water .pdf
 
Please answer all parts thank you!Parents PrPr YY rr x   PrPr.pdf
Please answer all parts thank you!Parents PrPr YY rr x   PrPr.pdfPlease answer all parts thank you!Parents PrPr YY rr x   PrPr.pdf
Please answer all parts thank you!Parents PrPr YY rr x   PrPr.pdf
 
Low platelet count it a recessively inherited trait. Reevaluation of .pdf
Low platelet count it a recessively inherited trait. Reevaluation of .pdfLow platelet count it a recessively inherited trait. Reevaluation of .pdf
Low platelet count it a recessively inherited trait. Reevaluation of .pdf
 
Match the modified stem to its correct definition.StolonRhizome.pdf
Match the modified stem to its correct definition.StolonRhizome.pdfMatch the modified stem to its correct definition.StolonRhizome.pdf
Match the modified stem to its correct definition.StolonRhizome.pdf
 
JAVAAdd to the code at the bottom to do the following two things.pdf
JAVAAdd to the code at the bottom to do the following two things.pdfJAVAAdd to the code at the bottom to do the following two things.pdf
JAVAAdd to the code at the bottom to do the following two things.pdf
 
Let (X,T) be a topological space,.A subset X. Suppose that for all x .pdf
Let (X,T) be a topological space,.A subset X. Suppose that for all x .pdfLet (X,T) be a topological space,.A subset X. Suppose that for all x .pdf
Let (X,T) be a topological space,.A subset X. Suppose that for all x .pdf
 
Choose the best possible answer for each of the following multiple ch.pdf
Choose the best possible answer for each of the following multiple ch.pdfChoose the best possible answer for each of the following multiple ch.pdf
Choose the best possible answer for each of the following multiple ch.pdf
 
If T is a bounded and self-adjoint operator on a Hilbert space and T_.pdf
If T is a bounded and self-adjoint operator on a Hilbert space and T_.pdfIf T is a bounded and self-adjoint operator on a Hilbert space and T_.pdf
If T is a bounded and self-adjoint operator on a Hilbert space and T_.pdf
 

Recently uploaded

Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of TransportBasic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of TransportDenish Jangid
 
e-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopale-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi RajagopalEADTU
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...Nguyen Thanh Tu Collection
 
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUMDEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUMELOISARIVERA8
 
diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....Ritu480198
 
8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital ManagementMBA Assignment Experts
 
SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code ExamplesPeter Brusilovsky
 
UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024Borja Sotomayor
 
Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxneillewis46
 
Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...EduSkills OECD
 
demyelinated disorder: multiple sclerosis.pptx
demyelinated disorder: multiple sclerosis.pptxdemyelinated disorder: multiple sclerosis.pptx
demyelinated disorder: multiple sclerosis.pptxMohamed Rizk Khodair
 
Trauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesTrauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesPooky Knightsmith
 
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...Nguyen Thanh Tu Collection
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsSandeep D Chaudhary
 
An Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge AppAn Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge AppCeline George
 
Book Review of Run For Your Life Powerpoint
Book Review of Run For Your Life PowerpointBook Review of Run For Your Life Powerpoint
Book Review of Run For Your Life Powerpoint23600690
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjMohammed Sikander
 

Recently uploaded (20)

Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of TransportBasic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
 
e-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopale-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopal
 
Including Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdfIncluding Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdf
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUMDEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
 
diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....
 
8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management
 
SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code Examples
 
UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024
 
Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptx
 
Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...
 
demyelinated disorder: multiple sclerosis.pptx
demyelinated disorder: multiple sclerosis.pptxdemyelinated disorder: multiple sclerosis.pptx
demyelinated disorder: multiple sclerosis.pptx
 
Trauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesTrauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical Principles
 
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
 
Supporting Newcomer Multilingual Learners
Supporting Newcomer  Multilingual LearnersSupporting Newcomer  Multilingual Learners
Supporting Newcomer Multilingual Learners
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
An Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge AppAn Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge App
 
VAMOS CUIDAR DO NOSSO PLANETA! .
VAMOS CUIDAR DO NOSSO PLANETA!                    .VAMOS CUIDAR DO NOSSO PLANETA!                    .
VAMOS CUIDAR DO NOSSO PLANETA! .
 
Book Review of Run For Your Life Powerpoint
Book Review of Run For Your Life PowerpointBook Review of Run For Your Life Powerpoint
Book Review of Run For Your Life Powerpoint
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
 

Lists can contain other lists as elements. For example the list HAIR.pdf

  • 1. Lists can contain other lists as elements. For example the list HAIRY=[1,2,[3,4,[5,[6,7.8],[9,10]],11,[12,[13,14],[15,16]]]] contains several lists containing other lists -- ie it is a "multilevel" list. Write a function flatten(L) which takes a possibly multi- level list L as an argument and returns a new list with the same elements in the same overall order as L but with only one level. For example, flatten(HAIRY) would return the list [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16] Solution Please find the required program along with its output. Please see the comments against each line to understand the step. def flatten(L): #recursive function to flatten a list for element in L: #iterate through each element of a list if not isinstance(element, list): # if the element is not a list, yield element # just return the element else: #if the element is a list, then recursivley call the flatten fucntion on the sub list, and return each element individually for x in flatten(element): yield x print "Flattened list elements are: " for x in flatten([1,2,[3,4,[5,[6,7,8],[9,10]],11,[12,[13,14],[15,16]]]]): print x, #print the flatten list elements ---------------------------------------- OUTPUT : Flattened list elements are: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16