On Tue, Dec 03, 2013 at 11:55:30AM -0600, Byron Ruffin wrote: > What I am having trouble with is finding a way to say: if lastName appears > more than once, print something. > > I ran a bit of code: > For x in lastname > If lastname = udall > Print something
You most certainly did not run that. That's not Python code. Precision and accuracy is vital when programming. Please tell us what you *actually* ran, not some vague summary which may or may not be in the right ballpark. Copy and paste is your friend here: copy and paste the block of code you ran, don't re-type it from memory. > This prints x twice. > > I think what I might be hung up on is understanding the ways that I can use > a loop. I know I need to loop through the list of names, which I have, and > set a condition dor the apppearance of a string occurring more than once in > a list but I don't know how to translate this to code. How do I say: if > you see it twice, do something? How do you know you've seen it twice? You have to remember the things you've seen before. The best way to do this is with a set, if possible, or if not, a list. already_seen = set() for name in last_names: if name in already_seen: print("Already seen", name) else: already_seen.add(name) Here's another way, not recommended because it will be slow for large numbers of names. (But if you only have a few names, it will be okay. for name in last_names: n = last_names.count(name) print(name, "appears %d times" % n) Can you combine the two so that the number of times a name appears is only printed the first time it is seen? -- Steven _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor