-2

I'm still trying to learn on python. I've created a list of fruit as follow

fruitList = ["pear","apple","strawberry","banana","orange"]

I would like to print them out in alphabetical order using for loop and while loop.

   fruitList.sort()

because python have this function called sort() thus, I'm able to sort. But what if I do not want to use the sort function?

However, right now, I've issue on how to sort using while loop? can someone tell me how am I able to do so? This is what I've tried, using len()

for fruit in fruitList:
    while (len(fruitList[0]) > len(fruit[0])):
            fruit += 1
            print fruit
            continue

Nothing is printed when I run this program. In a while loop, am I suppose to compare with the index[0] or can I use sort function as well?

Himani Trivedi
  • 158
  • 2
  • 3
  • 16
stack
  • 414
  • 1
  • 7
  • 21
  • 2
    Why do you need a for loop with built in sort function? I don't understand. – Ahsanul Haque Nov 30 '15 at 12:08
  • hello, im new to python , still trying to learn! – stack Nov 30 '15 at 12:10
  • 1
    You are trying to compare the following: not a list, but the first item of it, and not that, but the length of it. Let's be honest, that's very difficult to understand. Therefore I propose you: print as much information as possible (`fruitlist`, `fruitlist[0]`, `len(fruitlist[0])`, ...) and the solution will show itself. Good luck. – Dominique Nov 30 '15 at 12:11

5 Answers5

5

you can do so:

for f in sorted(fruitList):
    print(f)

and with while:

while fruitList:
    print(min(fruitList))
    fruitList.remove(min(fruitList))

But I mean, using "while" is a bad idea

P.S. sort() is not a function, It is a list method

  • Thanks. Can I ask, is min used to check for the first letter of a string ? – stack Nov 30 '15 at 12:29
  • You can have at look at this StackOverflow article in order to find out how to get general substrings from a string: http://stackoverflow.com/questions/663171/is-there-a-way-to-substring-a-string-in-python You can't use the `min()` function for this purpose. – Dominique Nov 30 '15 at 12:57
  • min() compares together objects of the same type, and return that which is minimal. Strings are compared lexicographically: first by char code, second, by string length – Alexander Safronov Nov 30 '15 at 14:06
1

The build in sort rutines does not require usage of neither for or While

If you want to learn for/while, look at some tutorials or something.

https://wiki.python.org/moin/ForLoop

To sort your list do something like this

fruitList = ["pear","apple","strawberry","banana","orange"]
print sorted(fruitList)
print fruitList
fruitList.sort()
print fruitList

output :

['apple', 'banana', 'orange', 'pear', 'strawberry']
['pear', 'apple', 'strawberry', 'banana', 'orange']
['apple', 'banana', 'orange', 'pear', 'strawberry']
Svend Feldt
  • 758
  • 4
  • 17
0

First of all you should chose a sort algorithm, IMO one of the most simple algorithms to understand is Bubble sort (But one of the most inefficient too!)

swaped = True #Just to enter the first time
while swaped:
    swaped = False
    for i in range(len(fruitList)-1):
        if fruitList[i] > fruitList[i+1]:
            aux = fruitList[i]
            fruitList[i] = fruitList[i+1]
            fruitList[i+1] = aux
            swaped = True
print(fruitList)
Mr. E
  • 2,070
  • 11
  • 23
0

OK, first of all you're trying to sort according to alphabetical, then why are you considering the length of the word on your list instead of their name?

Also, there are some mistakes in your code and I don't understand what you're trying to do.

for fruit in fruitList:
    while (len(fruitList[0]) > len(fruit[0])):
        fruit += 1
        print fruit
        continue

First, len(fruitList[0]) is the length of the first item in your list (let's say it's pear).

In the first iteration, fruit will be fruitList[0], pear.

When you're doing len(fruitList[0]) > len(fruit[0]) you're doing (in the first iteration) len("pear") > len('p') which is always 'True' as long as you don't have an empty string ... in which case fruit[0] would throw an error. Now, why are you doing this? What are you trying to do?

Anyway, let's move on, since the above condition was 'True', then the next line of code is fruit +=1 but fruit is a string and '1' is an int, so you're trying to add a 'str' + 'int' which raises an error.

Sooooo.... your code doesn't run, it throws an error, if you're saying that your code doesn't print anything, then you're getting 'False' in your while chunk, so the code you posted isn't the exact same thing you're running in your computer, am I right?

Finnally, to answer your question, to order your list in alphabetical order, you have to check for the letters.

First set the first item in the list as the first item. Then check the second item in your list and compare with the item before, if the second goes before, then move it. Then check the next item, compare with the item before it, if it goes before, then compare with the item before that one and move accordingly... repeat until you've check everyitem in the list. ---> Or use Bubble sort as explained above (just read that answer)

tglaria
  • 5,678
  • 2
  • 13
  • 17
-1

Now you iterate over the list, and for each fruit you sort the list again and again. You don't need this. Just do

print sorted(fruitList)
Viorel Roman
  • 120
  • 2