Computers and Technology

Write a function that accepts a list of lists and merges all of the items in the sublists into a single new list. The function should merge all of the elements (see example below) using only list comprehension, and return the newly created list. The original list (passed to the function) should not be modified in any way.

For example, in the code below, we create a list of lists. Each sublist may contain any type of data (and data in any sublist can be of different types).

This list of lists (some_list) is then passed to your function, which will return a new list. When printing the list that is returned by the function (printing is NOT performed in the function), you will see that the sublists have been merged into one overall list.

The order of the merge is as follows: first, all of the first sub-list's elements (in order they appeared), and then all of the second sub-list's elements, and so on.

For example, given the following code snippet,

some_list = [ [1, 2.0, True, "some string", 5], \
['NYU', False, (1, 2), {'joe':34.44, 'mary':67.55}, [1, 2, 3, 4, 5]] \
]
the sequence above would produce the following output:

[1, 2.0, True, 'some string', 5, 'NYU', False, (1, 2), {'joe': 34.44, 'mary': 67.55}, [1, 2, 3, 4, 5]]
Note carefully, that in the example above, the second sublist (that starts with the element 'NYU') has a fifth element that is a list (the list [1, 2, 3]). This fifth element is inserted into the function's resulting list without modification (it does not decompose the [1, 2, 3] list into smaller elements.

Extra Credit

For +5 points of extra credit: write the function such that the length of the sublists do not have to be the same. For example:

some_list = [ ['a','b'], [True, False, True], \
[1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9] \
]
print(merge_sub_lists(some_list)) would produce:

['a', 'b', True, False, True, 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9]
Note

Only solutions that use a list comprehension will be considered for any credit. Solving this problem with any other approach will not receive any credit.

Consider testing your solution with a variety of sample cases, not just the example provided above.

You may assume that the list will not be empty, nor will any element in the list be an empty list.

Hint

The function is literally a single statement. This is true for either solution (with or without the extra credit).

answer
Answers: 3

Other questions on the subject: Computers and Technology

image
Computers and Technology, 24.06.2019 07:40, daebreonnakelly
What type of multimedia are live news feeds? live news feeds are examples of multimedia.
Answers: 2
image
Computers and Technology, 24.06.2019 09:30, kyreesegordon
Retype the statements, correcting the syntax errors. system. out. println("num: " + songnum); system. out. println(int songnum); system. out. println(songnum " songs"); note: these activities may test code with different test values. this activity will perform two tests: the first with songnum = 5, the second with songnum = 9. see how to use zybooks.
Answers: 1
image
Computers and Technology, 24.06.2019 12:00, tipbri6380
An npn transistor is correctly biased and turned on if the a. base is negative. b. collector is negative. c. collector is positive with respect to the emitter and negative with respect to the base. d. collector is the most positive lead followed by the base.
Answers: 1
image
Computers and Technology, 24.06.2019 15:50, coralaguilar1702
Andy would like to create a bulleted list. how should he do this? andy should click on the bullet icon or select the bullet option from the menu and then type the list. andy should press the shift key and the 8 key at the beginning of each line of text. andy should type the text and then click on the bullet command. andy should press return and the bullets will automatically
Answers: 2
Do you know the correct answer?
Write a function that accepts a list of lists and merges all of the items in the sublists into a sin...

Questions in other subjects: