4
\$\begingroup\$

The aim of this program is to say which position a word is in a list. This is just a basic version of it. I made to see if I could shorten it more than what it is currently.

position, word, word_list = 0, 'hello', 'hello what is your name i am bob this is a sentence a very n ice sentence'
word_list = word_list.split(" ")
print(word_list)
if word in word_list:
    for name in word_list:
        position = position + 1
        if word == name:
            print('your word is in the list at ',position)
else:
    print('Your word is not in the sentence')
\$\endgroup\$
5
  • \$\begingroup\$ Alternately word_list.index(word) \$\endgroup\$
    – Peilonrayz
    Apr 10, 2016 at 14:12
  • \$\begingroup\$ @JoeWallis: .index() finds only the first match. \$\endgroup\$
    – zondo
    Apr 10, 2016 at 14:14
  • 1
    \$\begingroup\$ Welcome to CodeReview. As we all want to make our code more efficient or improve it in one way or another, try to write a title that summarizes what your code does, not what you want to get out of a review. Please see How to get the best value out of Code Review - Asking Questions for guidance on writing good question titles. \$\endgroup\$
    – tim
    Apr 10, 2016 at 14:20
  • \$\begingroup\$ @zondo didn't notice that, mb \$\endgroup\$
    – Peilonrayz
    Apr 10, 2016 at 14:27
  • 2
    \$\begingroup\$ Please don't change the code once you get an answer, as it invalidates the answer. See also What you may and may not do after receiving answers \$\endgroup\$
    – tim
    Apr 10, 2016 at 14:29

1 Answer 1

2
\$\begingroup\$

A similar solution using enumerate():

word, word_list = 'hello', 'hello what is your name i am bob this is a sentence a very n ice sentence'

if word in word_list:
    for position, name in enumerate(word_list):
        if name == word:
            print("Your word is in the list at ", position)
else:
    print('Your word is not in the sentence')

You could also use a list comprehension:

word, word_list = 'hello', 'hello what is your name i am bob this is a sentence a very n ice sentence'

positions = [x for x, n in enumerate(word_list).split() if n == word]
if positions:
    for position in positions:
        print('your word is in the list at ', position)
else:
    print('Your word is not in the sentence')

Also, a generator expression that's a little more efficient:

word, word_list = 'hello', 'hello what is your name i am bob this is a sentence a very n ice sentence'

found = False
for position in (x for x, n in enumerate(word_list) if n == word):
    print('your word is in the list at ', position)
    found = True
if not found:
    print('Your word is not in the sentence')
\$\endgroup\$
2
  • \$\begingroup\$ i have made some slight improvements by using procedures and some input statements. however when i ran it using the enumerate function it said the first word is position 0 which is due too computers count from 0 but how could i make it so it counts from 1? \$\endgroup\$ Apr 10, 2016 at 14:22
  • \$\begingroup\$ @Adam.Harris: Use print(..., position + 1) instead of print(..., position) You could also use enumerate(word_list, 1) instead of enumerate(word_list) \$\endgroup\$
    – zondo
    Apr 10, 2016 at 14:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.