jc13
jc13

Reputation: 85

How do you extract words in a list in python

So Im teaching myself how to program. So far I am going nowhere.

I have a code that from a line in a textfile, I put each word of that line in a list. Let's say the list is info =[john,guard,single]. I have successfully loaded the words into the list but have some problems with my next task. i want to get each word and use it in a sentence all i have done so far is

for word in info:
    print "My name is ",word[1]
    print "My job is " ,word[2]
    print "I am ",word[3]

but all i get is My name is o My job is h I am n can someone please help me?

ok so here's the code:

def loginfo(self):
    infolist = []
    with open('user.txt') as f:
        f = f.readlines()
        for line in f:
            if str(self.neim.get()) in line:
               if str(self.posse.get()) in line:
                  for word in line.split():
                      infolist.append(word)

    for word in infolist:
        print word[]

Upvotes: 0

Views: 21224

Answers (3)

kylieCatt
kylieCatt

Reputation: 11039

You don't need that for loop. You can simply:

print "My name is ",info[0]
print "My job is " ,info[1]
print "I am ",info[2]

In your loop you iterating over the list and assigning each string inside it to the variable word. you were then accessing the word with the index method []. Which is why you were getting individual letters instead of words. Also computers start counting at 0 not 1 so the indexes go:

[0,1,2,3]

So when you use a for loop like this:

for word in info:
    print(word[0])

What happens is:

  • The first element in the list is accessed which in your case is the string John
  • Next you call the index method on word word[0]
  • That will access and return the character in the 0th index which is the first character in the string
  • It will do that whole set of instructions for every element in the list

Upvotes: 1

9000
9000

Reputation: 40894

for word in info: is a loop. Its body (the indented part) will execute once for each element of info.

You could access info elements by index, as other answers suggest. You can also unpack the list into reasonably-named variables:

name, job, marital_status = info # the unpacking: 3 variables for 3 list items
print "My name is", name, "my job is", job, "I am", marital_status

The loop comes in handy when you have a list of people to process:

people = [  # a list of lists
  ["Joe", "painter", "single"],
  ["Jane", "writer", "divorced"],
  ["Mario", "plumber", "married"]
]

for person_info in people:
  print "Name:", person_info[0], "job:", person_info[1], "is", person_info[2]

It is idiomatic to unpack nested items right in the loop:

for name, job, marital_status in people:
  print "Person named", name, "works as a", job, "and is", marital_status

Upvotes: 0

dawg
dawg

Reputation: 103844

You could do something along these lines:

li=['My name is {}', "My job is {}", "I am {}"]
info=['john','guard','single']

for x, y in zip(li, info):
    print x.format(y)

Prints:

My name is john
My job is guard
I am single

Or, just use a single template:

print 'My name is {}\nMy job is {}\nI am {}'.format(*info)

Upvotes: 5

Related Questions