- 1

Coding HW

So uhhh what does my Coding Professor mean with this: 1. What is the output of: wordlist = ['cat','dog','rabbit'] letterlist = [ ] for aword in wordlist: for aletter in aword: letterlist.append(aletter) print(letterlist) Modify your code above so that only a single copy of each letter is output. I am confused as what he wants and don’t want to ask….

22nd Nov 2021, 1:42 AM
Caitlin Cook
Caitlin Cook - avatar
17 Réponses
+ 3
Caitlin Cook Caitlin Cook I agree, it took me a bit, but I believe he just wants to know the output of the original code for #1. Which should be each letter from the words seperated in the form a list: [c, a, t, d, o...]
22nd Nov 2021, 2:02 AM
Charles Jones
Charles Jones - avatar
+ 5
Caitlin Cook I have set out 3 snippets showing how to break it down. Uncomment to see the development words = ['cat','dog','rabbit'] letters= [ ] #for word in words: # print(word) #for word in words: # for letter in word: # print(letter) for word in words: for letter in word: if letter not in letters: letters.append(letter) print(letters)
22nd Nov 2021, 2:31 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 2
My best guess is that he wants you to check to see if a letter in the letterlist was already appended. You can do this by implementing the following condition before appending a letter: if aletter not in letterlist: # append aletter to letterlist ...
22nd Nov 2021, 1:50 AM
Charles Jones
Charles Jones - avatar
+ 2
Oooo he means no repeated letters
22nd Nov 2021, 2:55 AM
Caitlin Cook
Caitlin Cook - avatar
+ 1
Ipang oops typo thanks mate
22nd Nov 2021, 2:04 AM
Charles Jones
Charles Jones - avatar
+ 1
wordlist = ['cat','dog','rabbit'] letterlist = [ ] for aword in wordlist: for aletter in aword: letterlist.append(aletter) print(letterlist) def fix(letterlist): s = set() list = [] for ch in letterlist: if ch not in s: s.add(ch) list.append(ch) return ''.join(list) print(fix(letterlist))
22nd Nov 2021, 3:11 AM
Caitlin Cook
Caitlin Cook - avatar
+ 1
That't great, keep it
23rd Nov 2021, 6:44 PM
‎السفاح‎
0
It works!!!! Thanks so much!!
22nd Nov 2021, 3:11 AM
Caitlin Cook
Caitlin Cook - avatar
0
for aword in wordlist: for aletter in letterlist: letterlist.append(aletter) letterlist = set(letterlist) print(letterlist) A set contains only one copy of any element... So, casting as a set will do as well...
23rd Nov 2021, 3:29 AM
ISHIKA SHARMA
ISHIKA SHARMA - avatar
- 1
He is having me do that in the next problem so kinda confused on what he wants lol
22nd Nov 2021, 1:57 AM
Caitlin Cook
Caitlin Cook - avatar
- 1
Oooooo I get it now
22nd Nov 2021, 2:00 AM
Caitlin Cook
Caitlin Cook - avatar
- 1
Hmmm so the code does that already but not as pretty it does [‘c’, ‘a’, ‘t’…] and on
22nd Nov 2021, 2:06 AM
Caitlin Cook
Caitlin Cook - avatar
- 1
I feel like he had a typo maybe? Cause the code already does what he is asking me to do…right?
22nd Nov 2021, 2:53 AM
Caitlin Cook
Caitlin Cook - avatar
- 1
Thanks everyone for help figuring that out!!!
22nd Nov 2021, 3:08 AM
Caitlin Cook
Caitlin Cook - avatar
- 1
Good
23rd Nov 2021, 10:43 AM
P078
- 2
So like one letter from each word?
22nd Nov 2021, 2:00 AM
Caitlin Cook
Caitlin Cook - avatar