Question

Using Python Programming Language:

3. Write a function flatten that takes a 2D list and returns all the items of each list concatenated together into one new 1D

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

Define a function flatten() that accepts a 2D list

  1. define a function flatten()
  2. Initialize a new list return_list as empty list
  3. For each list in 2D list,
  4. Add all the elements in inner list to return_list using the funtion extend() of list class
  5. return return_list

Program:

Note: Please double-check the indentation using the screenshot provided as a reference.


def flatten(list_items):                # function faltten() that accepts a 2D list
        result_list = []                        # initialize return_list as empty list
        for item in list_items:         # iterate over each list in list_items
                result_list.extend(item) # add each items in inner list to result_list
        return result_list                      # return resurn_list

print(flatten([["a","b"],["c","d"],["e","f"]])) # prints ['a', 'b', 'c', 'd', 'e', 'f']

Screenshot:

def flatten(List_items): # function faltten() that accepts a 2D list result_list = [] # initialize return_list as empty list

Output:

[a, b, c, d, e, f]

Please don't forget to give a Thumbs Up.

Add a comment
Know the answer?
Add Answer to:
Using Python Programming Language: 3. Write a function flatten that takes a 2D list and returns...
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
  • Using Python Programming Language: 4. Implement the following function in the PyDev module functions.py and test...

    Using Python Programming Language: 4. Implement the following function in the PyDev module functions.py and test it from a PyDev module named t04.py: def print_matrix_char(matrix): Prints the contents of a 2D list of strings in a formatted table. Prints row and column headings. Use: print_matrix_char (matrix) Parameters: matrix - a 2D list of strings (2D list) Returns: None. Sample testing: Number of rows: 3 Number of columns: 4 0 1 2 3 i 0 с u r 1 с у...

  • Implement the following function in the PyDev module functions.py and test it from a PyDev module...

    Implement the following function in the PyDev module functions.py and test it from a PyDev module named t04.py: def print_matrix_char (matrix): وق في ال Prints the contents of a 2D list of strings in a formatted table. Prints row and column headings. Use: print_matrix_char (matrix) Parameters: matrix Returns None. a 2D list of strings (2D list) w Sample testing: Number of rows: 3 Number of columns: 4 0 1 2 3 i 0 C u r 1 Z 2 Y...

  • USING PYTHON PROGRAMMING LANGUAGE 15. Write code to open a file named data.txt, which contains 3...

    USING PYTHON PROGRAMMING LANGUAGE 15. Write code to open a file named data.txt, which contains 3 numbers, and print the sum of those numbers to the console 16. Write code that opens a file named words.txt containing an essay, and the prints out the SECOND word in the file to the console

  • "PYTHON" 1. a. Create a file (module) named camera_picture.py. b. Write a function, take_picture_effect, that takes...

    "PYTHON" 1. a. Create a file (module) named camera_picture.py. b. Write a function, take_picture_effect, that takes two parameters, the filename and an effect. c. Take a picture with the filename and effect. Use camera.effect to set the effect. Use help(PiCamera) to find the effects which you can apply. d. Create a second file use_camera.py. e. Import the take_picture_effect function from the camera_picture module. f. Prompt the user for the filename. g. Prompt the user for the image_effect. help(PiCamera) to see...

  • Write a Python function named print_nums that takes a single parameter, a list of float values,...

    Write a Python function named print_nums that takes a single parameter, a list of float values, and prints out the list on a single line.enclosed in brackets, each value with three places after the decimal point, the values separated by two spaces. You do not have to provide comments for this code. Example 1: print_nums([3/3, 4/3, 573, 6/3]) prints: [1.000 1.333 1.667 2.000] Example 2: print_nums([3]) prints: [3.000] Example 3: print_nums([]) prints: []

  • Please use python 3 programming language Write a function that gets a string representing a file...

    Please use python 3 programming language Write a function that gets a string representing a file name and a list. The function writes the content of the list to the file. Each item in the list is written on one line. Name the function WriteList. If all goes well the function returns true, otherwise it returns false. Write another function, RandomRange that takes an integer then it returns a list of length n, where n is an integer passed as...

  • PYTHON Programming short Questions: 1. Write a function takes as input name (string) and age (number)...

    PYTHON Programming short Questions: 1. Write a function takes as input name (string) and age (number) and prints: My name is <name> and I am <age> years old(where name and age are the inputs) 2. Write a function that takes as input a and b and returns the result of: ??ab 3. Write a function that takes as input a and b and returns the result of: ??ab if it is valid to divide a and b 4. Write a...

  • In Lisp programming language a 16. (20 pts.) Write a function that takes a list of...

    In Lisp programming language a 16. (20 pts.) Write a function that takes a list of 0's and l's and "toggles" each element. Your function should include lambda expression that knows how to flip an individual element, plus an applicative operator to do this to every element of the list. Example: 1 0) List (0 0 1 1 0 1) should be transformed into (1 1 0 0

  • Using python, Write a function max3 that takes three numbers as its arguments and returns the...

    Using python, Write a function max3 that takes three numbers as its arguments and returns the greatest of these three numbers. Your submitted file for this problem should include (a) function definition, obviously, and (b) the code that calls that function (for this problem you do not have to package that calling code into the main() function although you may if you want to), so that if I run your *.py file it computes and prints something (e.g. few test...

  • WRITE A CODE USING C PROGRAMMING LANGUAGE: PSUT planning to organize a Football champion by eight...

    WRITE A CODE USING C PROGRAMMING LANGUAGE: PSUT planning to organize a Football champion by eight teams where the first round has two groups and then the best two of then continue to the next round until we achieve the champion. A) The each round you will create a 2D array and you will use Random algorithm to determine the winner (the function still running until finding the best two teams) 0 lost 1 Drawn 2 win B) Then you...

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