N
N
Nock12021-07-25 09:58:10
Python
Nock1, 2021-07-25 09:58:10

Can only concatenate list (not "str") to list. What is the problem?

I made a bot, it was necessary to convert a two-dimensional list into a one-dimensional one . I
used:

def combing_list(self, task:list):
  result = sum(task, [])
  return result

users = self.combing_list(users)

And converted the type, and found out the type (list is always passed), but an error occurs.
TypeError: can only concatenate list (not "str") to list on the second line. What is the problem?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vindicar, 2021-07-25
@Nock1

What's on the list?
For your code to work, task must be not just a list, but a list of lists. If there is a line in it, then you will get exactly this error.
Heaps , it might be easier to use itertools.chain.from_iterable() to solve your problem .

S
soremix, 2021-07-25
@SoreMix

What do you want to achieve? If you add lists, then use append/extend.

O
o5a, 2021-07-25
@o5a

The fact that there is no such operation
sum(list1, list2)
. What did you want to do? If we add the data of another to one list, then
list1.extend(list2)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question