I'm trying to append a number to a list only when i > 5 and an empty string when it has no number greater than 5.

My problem is trying to append for the number of times equal to a single empty string and according to the first for loop for i in range(0,10)

This is what I've tried:

my_list = []

for i in range(0,10):
    for j in range (0,5):
        if i > 5:
            my_list.append(i)
        else:
            my_list.append('')

But I am getting the empty strings looping over both for-loops and I'm a bit confused on how to proceed.

The output should be a list with a total of 10 elements as:

output = ['', '', '', '', '', '', 6, 7, 8, 9]

Second example as the one above may have over-simplified things:

file_names_short = ['apple pie', 'apple cake', 'carrot apple', 'carrot cake']

threshold = 0.70
result_list = []
for x in file_names_short:
    for y in company_list:
        if similar(x, y) > threshold:
            result = x
            result_list.append(result)
        else:
            result_list.append('')

Which explains why it is necessary to have the second loop.

4 Answers

2
imposeren On Best Solutions

Looks like you want to get "Result should have '' for each file_name that does not match condition, and file_name if it matches condition":

Source = ['apple pie', 'apple cake', 'carrot apple', 'carrot cake']
#            |             |            |                   |
#           bad          Good          Bad                 Good
#            |             |            |                   |
Result = [   ''      , 'apple cacke',   ''          ,  'carrot cake']

Is that what you want?

my_list = []

for i in range(0,10):
    for j in range (0,5):
        if i > 5:
            my_list.append(i)
            break
    else:  # else of the `for`!!! not of the `if`
        # Condition did not match, so cycle was not broken - append ''
        my_list.append('')

threshold = 0.70
result_list = []

for x in file_names_short:
    for y in company_list:
        if similar(x, y) > threshold:
            result_list.append(x)
            break
    else:
        result_list.append('')
2
Devesh Kumar Singh On

Not sure why you have an extra inner for loop, when your outer loop does the job for you

my_list = []

for i in range(0,10):
    if i > 5:
        my_list.append(i)
    else:
        my_list.append('')
print(my_list)

The output is

['', '', '', '', '', '', 6, 7, 8, 9]

When you had your extra inner loop, every operation of my_list.append was being done 5 times for each i, hence you see a total of 35 spaces, and 5 numbers each! Which is

['', '', '', '', '', '', '', '', '', '', '', 
'', '', '', '', '', '', '', '', '', '', '', '', 
'', '', '', '', '', '', '', 6, 6, 6, 6, 6, 7, 
7, 7, 7, 7, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9]
0
ch4rl1e97 On

If I'm understanding correctly, you want to loop, for numbers 0 to 9 inclusive, and for 1, 2, 3, 4, 5 you want to insert a blank string, and for 6, 7, 8 and 9, you want to insert those numbers.

This does not require a double loop, I'm unsure where you got the inner/secondary loop idea from but its completely unneeded and is the exact reason you're getting more than what you asked for.

my_list = []

for i in range(0,10):
    if i > 5:
        my_list.append(i)
    else:
        my_list.append('')

print(my_list)

outputs:

['', '', '', '', '', '', 6, 7, 8, 9]
0
pdrersin On

The following should do it:

my_list = [i if i > 5 else '' for i in range(10)]

It outputs:

[" ", " ", " ", " ", " ", 6, 7, 8, 9]

Wish you the best.