SFC
SFC

Reputation: 793

Append to a list using a for loop

I am using the following code to assign a value to a given letter (A = +1, B= -1)

letList = [] 
let1 = ['A','A','B']
count = 0

for l in let1: 
    if l == "A": 
        count = count + 1
    else:
        count = count - 1

print(letList.append(count)) #doesnt work

Goal: I want to create a list of counts with the final output that looks something like this: letList = [1,2,1]

Problem: But when I try to append using letList.append(count) I get a none output

Suggestions?

Upvotes: 0

Views: 442

Answers (1)

Reginol_Blindhop
Reginol_Blindhop

Reputation: 355

You are trying to print the append function and the append needs to be in your loop to do what you want. Below is an example that prints [1,2,1] using append.

letList = []
let1 = ['A','A','B']
count = 0

for l in let1:
    if l == "A":
        count = count + 1
    else:
        count = count - 1

    letList.append(count)

print(letList) 

Upvotes: 1

Related Questions