mercredi 25 février 2015

how to append a FOR loop value into a list

Hello I am trying to append a value into a list and although there are various examples of this on here this is different I am using a FOR loop to go through all of the values into a list and if the right name is found in "each" I want to append "each" into a new list. Here is an example



for each in lines:
if names in each:
print(names)
print(each)
if len(newnames)<=3:
newnames.append((each))

elif len(newnames)>3:
newnames = newnames[:2]
print(newnames)


lines looks like this


['alex|7', 'jake|10', 'jake|2', 'jake|5', 'josh|3', 'josh|5', 'josh|7', 'megan|6', 'megan|8', 'megan|9']


Names is just a list with only one copy of each of these names. Hope this is enough information for you to be able to work out an answer because it has me completely stumped.


Cheers.


Aucun commentaire:

Enregistrer un commentaire