1

Convert a nested list from [[...],[...]] to [(...),(...)]. I wish to format my list below :

x=[['dog', 2], ['bird', 1],['dog',1]]

to

x=[('dog', 3), ('bird', 1)]

Here is my code for reference.

#Convert last element of nested list to int
newlist = [[int(element) if element.isdigit() else element for element in sub for sub in x]

#add the 2 columns that match 
grouped = dict()
grouped.update((name,grouped.get(name,0)+value) for name,value in newlist)
x = [*map(list,grouped.items())]

Could this be due to my use of a dict()

I have been successful with adding the second indices given that the first ones match, however the result is being formatted as such

x=[['dog', 3], ['bird', 1]]

however, I would like it as so any advice on how to get this ideal output?

x=[('dog', 3), ('bird', 1)]
Braiam
  • 1
  • 11
  • 47
  • 78

5 Answers5

3

I guess you are looking for collections.Counter:

from collections import Counter

x=[['dog', 2], ['bird', 1],['dog',1]]


c = Counter()

for k, v in x:
    c[k] += v

print(c)

# as pointed out by wim in the comments, use the below
# to get a list of tuples:

print([*c.items()])
khachik
  • 28,112
  • 9
  • 59
  • 94
1

Here is one way to do so:

x = [['dog', 2], ['bird', 1], ['dog', 1]]

data = {k: 0 for k, _ in x}
for key, num in x:
    data[key] += num
print(list(data.items()))  # [('dog', 3), ('bird', 1)]

You can also use setdefault():

data = {}
for key, num in x:
    data.setdefault(key, 0)
    data[key] += num
print(list(data.items()))
Cubix48
  • 2,607
  • 2
  • 5
  • 17
0

looks like this works

newlist = [int(element) if element[0].isdigit() else element for element in [sub for sub in x]]

# add the 2 columns that match
grouped = dict()
grouped.update((name, grouped.get(name, 0) + value) for name, value in newlist)
x = [*map(tuple, grouped.items())]
John R
  • 350
  • 2
  • 5
  • 19
0

Don't make it a list in the first place. The only real thing to note here is replacing list with tuple however, I also removed the unpacking ([*...]) and went directly to casting the parent as a list.

change: x = [*map(list,grouped.items())]

to: x = list(map(tuple, grouped.items()))

OneMadGypsy
  • 4,640
  • 3
  • 10
  • 26
-2
x=[['dog', 3], ['bird', 1]]
# You want it to be...
x=[('dog', 3), ('bird', 1)]

So you should first know how to convert ['dog', 3] to ('dog', 3):

>>> x = ['dog', 3]
>>> tuple(x)
('dog', 3)

To make it a tuple you just have to use the tuple's class constructor.


Then you have to apply this to the whole x list:

x = [tuple(i) for i in x]
FLAK-ZOSO
  • 3,873
  • 4
  • 8
  • 28
  • This doesn't seem to do anything to sum the common items. – wim Apr 11 '22 at 19:09
  • Right, I was just providing an information about the casting. But the OP is asking for the conversion of the result from lists to tuples, so what's the problem? – FLAK-ZOSO Apr 11 '22 at 19:11
  • You should check out the question again. They are asking how to convert a list with a variable number of `["dog", n]` for example and sum those n's into a single tuple. – theherk Apr 11 '22 at 19:16
  • "however, I would like it as so any advice on how to get this ideal output?" I was trying to help for this problem. – FLAK-ZOSO Apr 11 '22 at 19:26