2

I'm getting a TypeError: cannot concatenate 'str' and 'list' objects.

I'm trying to pass an object from a list to create a new variable by concatenating it with another variable.

Example: I want to take the value from the group list and concatenate it with "All.dbf" so it will do something with that file for each value in my list. If working properly, it would set the value for dbname equal to AdministrativeAll.dbf, CadastralAll.dbf and PlanimetericAll.dbf respectively each time it runs through but I am getting the 'str' and 'list' error....

group = ['Administrative', 'Cadastral', 'Planimetric']

for i in group:
    dbname = i + "All.dbf"

    blah, blah, blah....

I suppose I could add the "All.dbf" onto the values in the group list but thought there must be a better way to process this with a function or something that I don't know about....any thoughts??

Cheers

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
Jay
  • 325
  • 1
  • 5
  • 10
  • You're getting that error with that code? Are you sure you don't have `dbname = group + "All.dbf"`? – Wayne Werner Jul 08 '10 at 18:06
  • Pretty sure - I have my code as: group = ['Administrative', 'Cadastral', 'Planimetric'] for i in group: dbname = i + "All.dbf" – Jay Jul 08 '10 at 18:10
  • 2
    Are you sure that the error isn't coming from the code where it says 'blah, blah, blah'? Could you post the exact error and the traceback? – Mark Byers Jul 08 '10 at 18:11
  • I'm sure Mark - it fails directly at the Python command prompt when typed in manually and within the program itself. If I comment out that section (assigning group and the subsequent "for i in group:", it does everything else it is supposed to do... – Jay Jul 08 '10 at 18:15
  • 2
    try creating a new python file containing just three lines: `group = ['Administrative', 'Cadastral', 'Planimetric']; for i in group: print i + "All.dbf"` because *with that syntax* there is no way you should be getting an error. – Wayne Werner Jul 08 '10 at 18:20

4 Answers4

5

I suppose I could add the "All.dbf" onto the values in the group list but thought there must be a better way to process this with a function or something that I don't know about...

You could use a list comprehension:

group = [x + 'All.dbf' for x in group]
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
1
dbname = [i+"All.dbf" for i in group]
iamgopal
  • 8,806
  • 6
  • 38
  • 52
0

you will need to go though each item in the list and concatenate the two. A for each loop is your best bet. I'm not as familiar with python, so you might have to iterate over the list.

Forge
  • 501
  • 2
  • 9
0

Running this in 2.6.5:

group = ['Administrative', 'Cadastral', 'Planimetric']
for i in group:
    dbname = i + "All.dbf"
    print 'Concat', dbname

Gives the following output:

Concat AdministrativeAll.dbf
Concat CadastralAll.dbf
Concat PlanimetricAll.dbf

Worst case you can iterate through a range that is the length of the list, then do group[i] + "string to concatenate"

thegrinner
  • 11,546
  • 5
  • 41
  • 64