• Register
1 vote
913 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

Please log in or register to answer this question.

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

0 votes
1 answer 201 views
0 votes
1 answer 21 views
21 views
Problem: I am providing two values (mon_voltage and core_voltage) via the command line and I need to find a "starting point" from a two dimensional array where my iteration or loop will start from. Here is my code: myArray = [[1.02,1.13],[1.02,1.16],[1.02,1. ... : can only concatenate tuple (not "int") to tuple I can't figure out why start_point, which is an index, is a tuple. Please help.
asked Feb 13 sasha 13.2k points
0 votes
1 answer 110 views
1 vote
1 answer 1.1K views
1.1K 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, 2020 samhaz 5k points
1 vote
1 answer 77 views
77 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, 2020 adamSw 11.3k points
0 votes
1 answer 6 views
6 views
Problem: I've got two lists of coordinates, they look like this: list_kp2_ok: [[1185.60009765625, 933.6000366210938], [1310.4000244140625, 828.0000610351562], [1067.0, 979.0], [1310.0, 828.0], [1423.2000732421875, 814.800048828125], [1306.0, 828.0], [3634.0, 605.0 ... 828.6107788085938]] Now I need to check if there are any same coordinates on both lists and create a new list of them. So I wrote:
asked Feb 17 Mashhoodch 9.6k points
0 votes
1 answer 15 views
15 views
Problem: I have been banging my head against this for two days now. I am new to python and programming so the other examples of this type of error have not helped me to much. I am reading through the documentation for lists and tuples, but haven't found anything ... be much appreciated. Not looking for the answer necessarily, just more resources on where to look. I am using Python 2.7.6. Thanks
asked Jan 27 Mashhoodch 9.6k points
0 votes
1 answer 50 views
50 views
Problem: Hello! I am completely new to Python and trying to grasp concepts in Python. While I was learning lists and dictionaries, I wrote the following program: name = ["John", "Alice", "Catherine"] diction = {"name" : "Alice"} print(name[diction]) When I ... ; as a value and the program should print &ldquo;Alice&rdquo; on screen. I do not know what I have done wrong. Can anybody help?
asked Dec 18, 2020 Code Learner 6k points
0 votes
1 answer 30 views
1 vote
1 answer 692 views
692 views
Problem: I have a thousand line codes of python that I can not attach here. I am attaching the infected part from my whole code. This portion of code is producing the error. can only concatenate tuple (not "int") to tuple def calculateAns(num1, num2, ans, right): if ans ... ' right = right + 1 else: print 'Wrong' return right, ans I am looking forward to you guys to have a legit solution. Thanks.
asked Mar 30, 2020 Gavin 15.3k points