0

I am new to python and coding in general. Therefore I apologize if this has been talked about already and I am just unable to find it.

I am trying to get a list of stringnames.

for x in list:
    foldername = 'bla/exp-{number}/exp-{number}-A/bls'.format(number = x)

my_list.append(glob("/bla/bla/bla/" + foldername))
len(my_list)

len(my_list) returns just 2, even though x = 100

I am not really sure how to proceed.

Thanks in advance.

1
  • my_list.append is not inside the loop. Python uses indentation as syntax. Commented Jan 16, 2015 at 15:23

2 Answers 2

3

my_list.append(glob("/bla/bla/bla/" + foldername)) is called outside of the loop. So it will only append the last foldername.

Python is not my goto-language but

for x in list:
    foldername = 'bla/exp-{number}/exp-{number}-A/bls'.format(number = x)
    my_list.append(glob("/bla/bla/bla/" + foldername))

len(my_list)

Should give a diffrent result.

Sign up to request clarification or add additional context in comments.

Comments

2

You have to indent the line where you append to mylist:

for x in list:
    foldername = 'bla/exp-{number}/exp-{number}-A/bls'.format(number = x)
    my_list.append(glob("/bla/bla/bla/" + foldername))

len(my_list)

4 Comments

This will append the last foldername twice since it's still called outside of the loop as well. (Code rewritten now and no problem with it)
@dubbe yep, bad copy/paste :þ
Thankyou - by the way- why does this comment have to be longer than a certain amount of characters? I cannot just say 'thanks'. It is too short...
The idea is that you should upvote instead of saying just thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.