1

I've been trying to run some code to extract the values from a column and random sample some of them.
The problem is when I try to append values from the cursor to a list.
When I append values to the list, it works, but it appends the value inside a tuple and with a comma. Here is some sample of the code:

import arcpy
import random

arcpy.env.overwriteOutput = True
ws = r'D:\Projeto_VANT\SIG\proc_parc.gdb'
arcpy.env.workspace = ws

teste = r'teste'
with arcpy.da.SearchCursor(teste, "ID_TALHAO") as cursor:
    list = []
    for x in cursor:
        list.append(x)
    print(list)
    sample = random.sample(list, 2)
    print(sample)

And this is the results (list of values and the sample) I got which were printed:

[(5,), (13,), (20,), (30,), (38,)]
[(30,), (13,)]

How can I extract these values and append them to a list as integer, outside the tuple and without comma?

  • When you loop over a cursor it always yields a tuple of the same length as the number of fields you've asked for. Either unpack your single value immediately for x, in cursor: or specify the index within the tuple list.append(x[0]) – mikewatt Jun 23 at 22:42
  • Thanks @mikewatt it solved my problem! Thanks a lot! – caio.valente14 Jun 23 at 22:48
2

list as a variable name might get you into trouble caio, try to make it a habit to prefix (myVar, pVar etc) or proper case (ProperCaseExample) your variable names otherwise you might end up overwriting a keyword. Python is lax in its protection of keywords and will not even issue a warning if you do overwrite one. Most python keywords are all lower case, True and False being notable exceptions, therefore declaring your variable names in any case other than lower case reduces the risk of problems later. Consider this:

list         # list is a keyword, python already knows what it is
<type 'list'>
list()       # returns a new, empty list but could also convert a tuple into a list
[]
list = [1,2] # oops, we've just overwritten list with a value
list         # now list is no longer <type 'list'> it's now [1,2]
[1, 2]
list()       # as list has been overwritten you can no longer use it as before
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
TypeError: 'list' object is not callable

Because you've overwritten the builtin object list if you later decide to use list(myTuple) to convert a tuple into a list, a handy approach as tuples are read only and if you want to change or remove values do so as a list object then convert back to a tuple. Now that list is overwritten python is going to throw an exception and you will struggle for a very long time trying to find the error in syntax that checks out 100% with every reference and the problem could be hundreds of lines before.

Another option to append elements into a list that works for me is list += x which works if x is a tuple or list. The addition assignment operator (+=) also works for lists of more than one element:

myList = [1,2] 
myList += [3,4] 
myList is now [1, 2, 3, 4] 

Append has a different behavior with lists:

myList = [1,2] 
myList.append([3,4])
myList is now [1, 2, [3, 4]]

Which is probably not what was intended but is great if you know this quirk and are prepared for list or tuple items in your list.

In your code:

with arcpy.da.SearchCursor(teste, "ID_TALHAO") as cursor:
    list = []
    for x in cursor:
        list += x
    print(list)
    sample = random.sample(list, 2)
    print(sample)

or as a list comprehension:

list = [x[0] for x in arcpy.da.SearchCursor(teste, "ID_TALHAO")] 

Note that the addition assignment is not valid here, you need to use indexing as Hornbydd indicates in his answer.

| improve this answer | |
  • I appreciate your tips! Since I have just started with programming every tip is helpful! About what you said of naming the a variable with the word "list", I was only providing a sample of my problem, it is not what is written in the code, but thanks for the helpful tips anyway, I didn't know very well how things occur "behind the scenes", in the environment. Also, thanks for explained the two ways of doing what I want! Congratulations for your work. – caio.valente14 Jun 26 at 21:33
5

Replace list.append(x) with list.append(x[0])

x is a tuple of 1 and when you add those to lists or convert to strings you get that (x,) notation.

| improve this answer | |
  • Thanks a lot @Hornbydd it worked very well for me! – caio.valente14 Jun 23 at 22:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.