28.6k views
2 votes
Python Function Help Please!!!!

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).

User Jimeh
by
7.0k points

1 Answer

4 votes

Answer:

  1. def mergeList(aList):
  2. merge = [item for sublist in aList for item in sublist]
  3. return merge
  4. some_list = [[1, 2.0, True, "some string", 5], ['NYU', False, (1, 2), {'joe':34.44, 'mary':67.55}, [1, 2, 3, 4, 5]]]
  5. print(mergeList(some_list))
  6. 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]]
  7. print(mergeList(some_list))

Step-by-step explanation:

Firstly create a function mergeList that take one input list (Line 1). In the function user list comprehension to merge all the sublist from the input list into a single list (Line 2). This list comprehension will loop through every item in the main list and followed by every item in the sublist and then add it as an item in the new single list. At last, return the merge list (Line 3).

We can test our function using the two sample lists (Line5-9) and the output are

[1, 2.0, True, 'some string', 5, 'NYU', False, (1, 2), {'joe': 34.44, 'mary': 67.55}, [1, 2, 3, 4, 5]]

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

User Steve Madsen
by
7.2k points