Python Forum
Iterating over word vs. character
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Iterating over word vs. character
#1
This:

mutants = ['charles xavier',
            'bobby drake',
            'kurt wagner',
            'max eisenhardt',
            'kitty pryde']
for word in mutants:
    print(word)
outputs

charles xavier
bobby drake
kurt wagner
max eisenhardt
kitty pryde
Why does this not iterate one letter as a time vs. one word at a time?
Reply
#2
I don't really know what else you expect. You're iterating over a list, which means you get each item in that list in turn and those items are strings. If you want to iterate over the characters in each string, then you need another loop to do that.
Reply
#3
You can use the join() method to join all the strings into one.
Reply
#4
@ndc85430 answered to the WHY question.
You can get the desired output by using itertools.chain or using iterating over elements and then iterating over each element

from itertools import chain

mutants = ['charles xavier',
            'bobby drake',
            'kurt wagner',
            'max eisenhardt',
            'kitty pryde']
for char in chain(*mutants):
    print(char)

#alternative
for name in mutants:
    for char in name:
        print(char)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
(Aug-11-2020, 11:56 AM)ndc85430 Wrote: I don't really know what else you expect. You're iterating over a list, which means you get each item in that list in turn and those items are strings. If you want to iterate over the characters in each string, then you need another loop to do that.

That makes total sense. Thanks!

I'm learning Python through online classes. I get short videos on all kinds of different topics with exercises aimed to test the specific material. I don't get a lot of unifying material aimed at pulling things together and exposing some of these nuances that make all the difference.
Reply
#6
I guess it's good you're asking these questions then, but what I'd also urge you to do at this stage is to solve a lot of programming problems so you can start to work these things out. Codewars is good for this and they have different levels of problems.
Reply
#7
(Aug-11-2020, 02:09 PM)ndc85430 Wrote: I guess it's good you're asking these questions then, but what I'd also urge you to do at this stage is to solve a lot of programming problems so you can start to work these things out. Codewars is good for this and they have different levels of problems.

I'll check that out. Thanks!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Problem: Check if a list contains a word and then continue with the next word Mangono 2 2,488 Aug-12-2021, 04:25 PM
Last Post: palladium
  [solved] unexpected character after line continuation character paul18fr 4 3,392 Jun-22-2021, 03:22 PM
Last Post: deanhystad
  SyntaxError: unexpected character after line continuation character siteshkumar 2 3,163 Jul-13-2020, 07:05 PM
Last Post: snippsat
  Python Speech recognition, word by word AceScottie 6 15,985 Apr-12-2020, 09:50 AM
Last Post: vinayakdhage
  how can i handle "expected a character " type error , when I input no character vivekagrey 2 2,733 Jan-05-2020, 11:50 AM
Last Post: vivekagrey
  print a word after specific word search evilcode1 8 4,817 Oct-22-2019, 08:08 AM
Last Post: newbieAuggie2019
  Replace changing string including uppercase character with lowercase character silfer 11 6,179 Mar-25-2019, 12:54 PM
Last Post: silfer
  difference between word: and word[:] in for loop zowhair 2 3,664 Mar-03-2018, 07:24 AM
Last Post: zowhair
  SyntaxError: unexpected character after line continuation character Saka 2 18,548 Sep-26-2017, 09:34 AM
Last Post: Saka

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020