1

I'm getting an error while I'm writing my code

tmp = c.pop(0) # taking out the first element from the list as the starting point
complete = tmp[0][-1]
print(complete)

while c != []:
    complete += tmp[1][-1]
    [index] = []
    for i, pair in enumerate(c):
        if pair[0] == tmp[1]:
            [index].append(i)
    temp = c.pop(index)

print(complete)

I'm getting this error in the [index] == [] part:

Traceback (most recent call last):
ValueError: need more than 0 values to unpack

My question is, why is this error coming up and how should I fix this?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Jessi
  • 1,378
  • 6
  • 17
  • 37

1 Answers1

3

In Python, even if the variable contains a list, it should be used as a regular variable like index = []. When you use [index] = [] Python thinks you want to assign the first item of the list to your variable.Likewise, when you use the code [first, last] = [1,2], then the variable first is assigned 1 and the variable last is assigned 2. This is called unpacking.

Also, in [index].append(2), you should not use the square brackets around the variable name. This would not raise any errors, but what it would do is create a new list (with the sole item being the value of index) and then destroy the list when the line has executed.

Your code should look like this (Assuming the other parts of your code are correct). Also, as this comment suggested, use c instead of c != [] because an empty list is false and it follows Python conventions. :

tmp = c.pop(0) # taking out the first element from the list as the starting point
complete = tmp[0][-1]
print(complete)

while c: # c != []
    complete += tmp[1][-1]
    index = [] # [index] = []
    for i, pair in enumerate(c):
        if pair[0] == tmp[1]:
            index.append(i) # [index].append(i)
    temp = c.pop(index)

print(complete)

As noted in the comments, the line temp = c.pop(index) will give an error because pop expects an integer and the code is giving it a list.

However, because of the OP's usage of index in the code, I think he meant to use index as a integer. Also, the OP stated that the usage of temp instead of tmp was a mistake.

tmp = c.pop(0) # taking out the first element from the list as the starting point
complete = tmp[0][-1]
print(complete)

while c:
    complete += tmp[1][-1]
    index = 0
    for i, pair in enumerate(c):
        if pair[0] == tmp[1]:
            index = i
    tmp = c.pop(index)

print(complete)
Community
  • 1
  • 1
jkd
  • 1,045
  • 1
  • 11
  • 27
  • It seems to me that the convention is to use no enclosing punctuation (or parentheses, if you must) on the left side of an unpacking assignment. Also, use `if c` instead of `if c != []`, and check out the [official Python style guide](https://www.python.org/dev/peps/pep-0008/). – Blacklight Shining Apr 12 '15 at 01:42
  • @jake, by taking the [] out from index, i get TypeError: 'list' object cannot be interpreted as an integer, I might try to navigate this code in some other way i guess. – Jessi Apr 12 '15 at 01:46
  • @Jessi In your while loop. The `if` is implied. – jkd Apr 12 '15 at 01:47
  • @Jessi For your second error, which line? BTW, surround text with backquotes ( ` ) to make something in `code`. – jkd Apr 12 '15 at 01:49
  • @Jessi Oh, I see. The `temp = c.pop(index)` line is giving you that error. That is because you are trying to pass in a list when the function is expecting a integer. I have no idea what you were trying to do in that line. Can you explain? – jkd Apr 12 '15 at 01:52
  • @jake temp = c.pop(index) <- this line, since I'm appending my index with i(value), it won't let me use index inside c.pop – Jessi Apr 12 '15 at 01:53
  • @Jessi I think you mean to use an integer variable for `index` . – jkd Apr 12 '15 at 01:56
  • @jake I'm using nested lists [[],[],[],[]....] like this and I'm taking thesub list out of my outer list – Jessi Apr 12 '15 at 01:58
  • I would recommend changing your names to make them better reflect their values—don't use `index` for a list of lists, for example. – Blacklight Shining Apr 12 '15 at 01:59
  • @ jake, yea but its my first time using the stackflow, so i'm not familiar with the interface, should I copy to here? – Jessi Apr 12 '15 at 02:04
  • @Jessi Have you read the little links that pop up when you post a question? Ex: http://stackoverflow.com/help/how-to-ask. For me, the top things you can have are: the code you need to replicate it, example user inputs and expected outputs, **Full** TraceBack errors, purpose and example values for variables not declared in the code. – jkd Apr 12 '15 at 02:07
  • c = [['TACA', 'ACAG'], ['CAGA', 'AGAT'], ['AGAT', 'GATT'], ['ATTA', 'TTAC'], ['ACAG', 'CAGA'], ['GATT', 'ATTA'], ['TTAC', 'TACA']] and the expected results are GATTACA, ATTACAG, TTACAGA,TACAGAT,ACAGATT,CAGATTA,AGATTAC any one of this since I'm using set() in the top of my code, I'm trying to concatenate segments of DNA to make a complete sequence of DNA. – Jessi Apr 12 '15 at 02:10
  • @Jessi is it intentional that you use both `tmp` and `temp` in your code? – jkd Apr 12 '15 at 02:15
  • @ jake no , a mistake, it is tmp – Jessi Apr 12 '15 at 02:17
  • 1
    I'm getting the right answer, thanks for the help jake, awsome help!! – Jessi Apr 12 '15 at 02:20
  • 1
    @Jessi Great time with you too. Next time, try to include more information into your question. You gave a lot of useful information in the comments that should have included in the question – jkd Apr 12 '15 at 02:23