# TypeError: unsupported operand type(s) for -: 'list' and '

Page 1 of 1

## 5 Replies - 9421 Views - Last Post: 22 August 2012 - 06:52 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=288878&amp;s=823189b71aeaa5e49612f3843a4f9850&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 krielovas

Reputation: 1
• Posts: 5
• Joined: 14-August 12

# TypeError: unsupported operand type(s) for -: 'list' and '

Posted 14 August 2012 - 03:12 PM

```def bubble(n):
change = False
while change == False:
for p in range(n - 1):
j1 = list[p]
j2 = list[p + 1]
if j1 > j2:
list[p] = j1
list[p + 1] = j2
else:
list[p] = j2
list[p + 1] = j1
change = true
return(n)
import random
number = []
for a in range(20):
number.append(random.randint(1, 100))
bubble(number)

```

Traceback (most recent call last):
File "C:\eclipse\workspace\bubble test 1\test 1", line 20, in <module>
bubble(number)
File "C:\eclipse\workspace\bubble test 1\test 1", line 5, in bubble
for p in range(n - 1):
TypeError: unsupported operand type(s) for -: 'list' and 'int'

Help me understand what I am doing wrong.

This post has been edited by atraub: 14 August 2012 - 07:45 PM
Reason for edit:: fixed code tags

Is This A Good Question/Topic? 0

## Replies To: TypeError: unsupported operand type(s) for -: 'list' and '

### #2 sepp2k

• D.I.C Lover

Reputation: 2180
• Posts: 3,368
• Joined: 21-June 11

## Re: TypeError: unsupported operand type(s) for -: 'list' and '

Posted 14 August 2012 - 03:52 PM

number is a list containing 20 random numbers. You're giving that list as an argument to bubble. So when bubble runs, n is that same list of 20 numbers. Then on line 5 you write n - 1.

Generally when you use - both operands should be a number (or some other types that can be subtracted from each other). Here you're trying to subtract the number 1 from a list. As the error message is telling you, you can't do that.

### #3 Martyr2

• Programming Theoretician

Reputation: 4521
• Posts: 12,488
• Joined: 18-April 07

## Re: TypeError: unsupported operand type(s) for -: 'list' and '

Posted 14 August 2012 - 04:07 PM

Not to mention your algorithm is not correct. Everywhere you mention list[] it should be n[] and also if j1 > j2 why are you setting list[p] to j1? It should be set to j2 and list[p+1] should be j1.

Because the way you have it you are essentially doing list[p] = list[p].

Also you don't need the else in there. If j1 is greater than j2, switch them and go around for another pass.

Lastly, take a look at how you are using your boolean flag. I think you might be missing a crucial step in it.

### #4 krielovas

Reputation: 1
• Posts: 5
• Joined: 14-August 12

## Re: TypeError: unsupported operand type(s) for -: 'list' and '

Posted 16 August 2012 - 04:22 PM

What I'm trying to do is make a bubble sort where p equals a number in the list and p + 1 equals the next number in the list.

### #5 baavgai

• Dreaming Coder

Reputation: 6053
• Posts: 13,107
• Joined: 16-October 07

## Re: TypeError: unsupported operand type(s) for -: 'list' and '

Posted 16 August 2012 - 05:52 PM

You've been told rather clearly what's going on. However, I do like a good example.
```>>> # list is defined
... number = [5, 2, 12, 8]
>>> number
[5, 2, 12, 8]
>>> # we give it an even stupider name
... n = number
>>> n
[5, 2, 12, 8]
>>> # see if this means anything
... n - 1
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
TypeError: unsupported operand type(s) for -: 'list' and 'int'
>>> # clearly this won't
... range(n - 1)
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
TypeError: unsupported operand type(s) for -: 'list' and 'int'
>>> # just for yucks, is this anything?
... list[0]
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
TypeError: 'type' object is unsubscriptable
>>> # now, if it were this we might be on to something
... n[0]
5
>>> # this also has meaning
... len(n)
4
>>>

```

Hope this helps.

### #6 krielovas

Reputation: 1
• Posts: 5
• Joined: 14-August 12

## Re: TypeError: unsupported operand type(s) for -: 'list' and '

Posted 22 August 2012 - 06:52 PM

Fixed

```def bubble(n):
finished = False
while not finished:
finished = True
for p in range(0, len(n)-1):
if n[p] < n[p+1]:
left = n[p]
right = n[p+1]
n[p] = right
n[p+1] = left
finished = False
print(n)
#
import random
number= []
for a in range(20):
number.append(random.randint(1, 200))
#
print(number)
#
bubble(number)

```