1

If I want to combine lists inside list based on element value how can I achieve that? suppose if list

lis = [['steve','reporter','12','34','22','98'],['megan','arch','44','98','32','22'],['jack','doctor','80','32','65','20'],['steve','dancer','66','31','54','12']]

here list containing 'steve' appears twice so I want to combine them as below

new_lis = [['steve','reporter','12','34','22','98','dancer','66','31','54','12'],['megan','arch','44','98','32','22'],['jack','doctor','80','32','65','20']]

I tried below code to achieve this

new_dic = {}
for i in range(len(lis)):
    name = lis[i][0]
    if name in new_dic:
        new_dic[name].append([lis[i][1],lis[i][2],lis[i][3],lis[i][4],lis[i][5]])
    else:
        new_dic[name] = [lis[i][1],lis[i][2],lis[i][3],lis[i][4],lis[i][5]]
print(new_dic)
    

I ended up creating a dictionary with multiple values of lists as below

{'steve': ['reporter', '12', '34', '22', '98', ['dancer', '66', '31', '54', '12']], 'megan': ['arch', '44', '98', '32', '22'], 'jack': ['doctor', '80', '32', '65', '20']}

but I wanted it as single list so I can convert into below format

new_lis = [['steve','reporter','12','34','22','98','dancer','66','31','54','12'],['megan','arch','44','98','32','22'],['jack','doctor','80','32','65','20']]

is there a way to tackle this in different way?

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
xionxavier
  • 47
  • 7
  • Use `extend()` instead of `append()` when `name` exists. Also, why are you writing out all elements of `lis[i]`? Just do `lis[i][1:]` to slice the list from the second element onwards. Finally, don't use `for i in range(len(lis))` and `lis[i]`. Just do `for item in lis` and use `item` instead of `lis[i]`. – Pranav Hosangadi Aug 19 '21 at 16:04
  • Does this answer your question? [Take the content of a list and append it to another list](https://stackoverflow.com/questions/8177079/take-the-content-of-a-list-and-append-it-to-another-list) – Pranav Hosangadi Aug 19 '21 at 16:06
  • Also, welcome to Stack Overflow :) This is a well-asked question because you've described the problem clearly, given expected input and output, included your code, and asked a specific question. One recommendation I have is to ask _about the specific problem you're having_ instead of the goal of your program. In this case, your question is "How do I add all elements of one list to another list", and that question has already been answered many times before. Helpful SO links: [tour], [ask], [mre], and the [question checklist](//meta.stackoverflow.com/q/260648/843953) – Pranav Hosangadi Aug 19 '21 at 16:09
  • I'm not sure what the numbers in your list relate to, but they seem to be associated with the prior reporter/dancer role/designation. The way you're currently storing this data, and the way you propose to aggregate data for multiple roles, is less than ideal. A simple dict where the top-level keys are names e.g. 'steve' and each value is a dict of lists e.g. `{ 'steve': { 'reporter': [12,34,22,98], 'dancer': [66,31,54,12] } }` might be better. – jarmod Aug 19 '21 at 16:19

2 Answers2

0

There is a differnet way to do it using groupby function from itertools. Also there are ways to convert your dict to a list also. It totally depends on what you want.

from itertools import groupby

lis = [['steve','reporter','12','34','22','98'],['megan','arch','44','98','32','22'],['jack','doctor','80','32','65','20'],['steve','dancer','66','31','54','12']]

lis.sort(key = lambda x: x[0])
output = []
for name , groups in groupby(lis, key = lambda x: x[0]):
    temp_list = [name]
    for group in groups:
        temp_list.extend(group[1:])
    output.append(temp_list)

print(output)

OUTPUT

[['jack', 'doctor', '80', '32', '65', '20'], ['megan', 'arch', '44', '98', '32', '22'], ['steve', 'reporter', '12', '34', '22', '98', 'dancer', '66', '31', '54', '12']] 
Albin Paul
  • 3,330
  • 2
  • 14
  • 30
0

Not sure whether this snippet answers your question or not. This is not a fastest approach in terms to time complexity. I will update this answer if I can solve in a better way.

lis = [['steve','reporter','12','34','22','98'],['megan','arch','44','98','32','22'],['jack','doctor','80','32','65','20'],['steve','dancer','66','31','54','12']]
new_lis = []
element_value = 'steve'
for inner_lis in lis:
    if element_value in inner_lis:
        if not new_lis: 
            new_lis+=inner_lis
        else:
            inner_lis.remove(element_value)
            new_lis+=inner_lis
        lis.remove(inner_lis)

print([new_lis] + lis)

Output

[['steve', 'reporter', '12', '34', '22', '98', 'dancer', '66', '31', '54', '12'], ['megan', 'arch', '44', '98', '32', '22'], ['jack', 'doctor', '80', '32', '65', '20']]
environ
  • 1
  • 1