• Register
1 vote
472 views

Problem:

I have a very simple question for you guys. What does the error: can only concatenate list (not "int") to list mean? Well, let me clarify my problem a little bit more. I am trying to add to lists in python, but it’s not working. Yeah, I am a new bloody in python.

Here is my snippet

number=[1, 2, 3]
number[1:]+number[0]

Where did I wrong in my program? Your kind solution is much appreciated.

Thanks mate!

12 7 7
15,250 points

2 Answers

2 votes

Solution:

You can use a (+) operator to add two different lists in python. But you can’t add two different lists this way. Because lists are objects, we can call methods on a list. One method, .append() takes a single parameter and adds it to the end of a list.

You tried to slicing the list but the number[0] is an integer here not a list. You may try:

number = [1,2,3,4,5,6]
number1 = nnumber[2:]+number[3:]
print(number1)

Thanks

13 9 6
94,240 points
0 votes

Solution:

s[j] = max(b)

doesn't treat b as a 2-d array of integers and pick the biggest one. b is a list of lists. max(b) compares the lists and returns the one that compares highest. (List comparison is done by comparing the elements lexicographically.)

s[j] = max(max(sublist) for sublist in b)

I got the same error with the following python code:

 retList = []
    for anItem in aList:
        if anItem % 2 == 0:
            retList = retList + anItem
    return retList

when I changed the "+" which I used for concatenation to an append statement:

retList = []
    for anItem in aList:
        if anItem % 2 == 0:
            retList.append(anItem) 
    return retList

It will works fine.

or you can try this code

b=[[0 for m in range(1000)] for n in range(1000)]
    for j in xrange(2,num):
        for i in xrange(0,j):
             b[j][i] = s[j][i] + min(int(aliens[j]),f[j-i])

It seems to me likes is a 2D list (list of a list), and thus, you can't perform the operation.

s[j] + min(int(aliens[j]),f[j-i])

 

10 6 4
31,120 points

Related questions

1 vote
1 answer 470 views
470 views
Problem: I made a tuple to add to my main program . But this thing does not seem to work . I wrote this code: print("You should study hard to pass the exam") exam=("Study","Pc","python") print ("The things I have :") for stuff in exam: print (stuff) ... <module> exam += extra TypeError: can only concatenate tuple (not "str") to tuple Can anyone sort out this problem and help me out to solve this?
asked Apr 21 samhaz 5k points
1 vote
1 answer 14 views
14 views
Problem: I am a new student in Python programming. I have made a program that is supposed to take multiple inputs from the user and then print simply. Yeah, I know that sounds pretty easy but I am facing some kind of TypeError from the program. I am attaching the ... am getting is: TypeError: can only concatenate str (not "int") to str What&rsquo;s wrong here? Thanks for your kind help, mate.
asked Jun 29 adamSw 11.3k points
0 votes
1 answer 10 views