combine list values with common keys to a dictionary

Question:

I have the a list like

{
‘person1’, ‘Hi’,
‘person1’, ‘You there?’,
‘person2’, ‘Hello’,
‘person1’, ‘Hru?’,
‘person2”,’Good’
}

How do i convert this into a dictionary by grouping keys and combining text values?
Expected result:

{‘person1’: ‘Hi You there? Hru?’,
‘person2’: ‘Hello Good’}

NOTE: The grouping should be done using the key names. As the conversation isn’t always person1 followed by person2.

The code from aniketh seems to work. I now need to apply this on a dataframe which has many of this lists.

      chatid   list_of_convo
--------------------------------------------------------------------
0     12         {'person1', 'Hi','person1', 'You there?','person2', 
                  'Hello','person1', 'Hru?','person2','Good'}
1     21          {'person3', 'Hi','person3', 'You there?','person2', 
                   'Hello','person3', 'Hru?','person2','Good'}
2     34          {'person5', 'Hi','person5', 'You there?','person2', 
                   'Hello','person5', 'Hru?','person2','Good'}

The answer to above is:

def Convert(lst):
    dct = {}
    for ind in range(len(lst)):
      if ind % 2 == 0:
        if lst[ind] not in dct:
          dct[lst[ind]] = ""

    for ind in range(len(lst)):
      if ind % 2 == 1:
        dct[lst[ind - 1]] += lst[ind] + " "

    return dct

df['grouped'] = df['list_of_convo'].apply(Convert)

(For other’s reference)

Asked By: Shay

||

Answers:

Try:

pairs = zip(my_list[::2], my_list[1::2])
mydict = {}

for k, v in pairs:
   mydict[k] = (mydict.get(k, '') + ' ' + v).strip()

my_list[::2] will get every other element of the list starting at the first element (which should be your keys). my_list[1::2] will do the same but starting on the second element and this should be your values. zip() will make a list of tuples that pair each key and value. Then the last line just parses that list of key-value tuples into an actual dict based on your requirements around repeat keys.

Answered By: lordnoob

The following code could work:

lst = [ 'person1:', 'Hi', 'person2:', 'Hello', 'person1:', 'Hru?', 'person2:','Good' ]

dct = {}
for ind in range(len(lst)):
  if ind % 2 == 0:
    if lst[ind] not in dct:
      dct[lst[ind]] = ""

for ind in range(len(lst)):
  if ind % 2 == 1:
    dct[lst[ind - 1]] += lst[ind] + " "

print(dct)

Output:

{'person1:': 'Hi Hru? ', 'person2:': 'Hello Good '}

First, to initialize a list, we use [] instead of {}

After we make our dictionary, dct, we can iterate through every even indexed element of lst to get the elements ('people1', 'people2') that will be the keys of dct. First, we check if the key already exists in dct, and if not, we add it in.

Finally, we iterate through the odd indexed elements and concatenate them to the values of their respective keys (the element before them in lst).

I hope this helped! Please let me know if you need any further help or clarification!

Note: If you care about the extra space in the output, add the following code segment before you print to get rid of the extra space:

for key, value in dct.items():
  dct[key] = value[:-1]

Second Input:

lst = ['person1', 'Hi', 'person1', 'You there?', 'person2', 'Hello', 'person1', 'Hru?', 'person2','Good' ]

Output:

{'person1': 'Hi You there? Hru?', 'person2': 'Hello Good'}

Answered By: Aniketh Malyala
lst = ['person1', 'Hi', 'person2', 'Hello', 'person1', 'Hru?', 'person2', 'Good']
d = {}
for k, v in zip(lst[:-1:2], lst[1::2]):
    if k in d:
        d[k].append(v)
    else:
        d[k] = [v]

Result is:

{'person1': ['Hi', 'Hru?'], 'person2': ['Hello', 'Good']}
Answered By: AGN Gazer
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.