Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python list order

In the small script I wrote, the .append() function adds the entered item to the beginning of the list, instead of the end of that list. (As you can clearly understand, am quite new to the Python, so go easy on me)

list.append(x)
Add an item to the end of the list; equivalent to a[len(a):] = [x].

That's what is says in https://docs.python.org/2/tutorial/datastructures.html.

You can see my code below:

user_input = []
def getting_text(entered_text):
    if entered_text == "done":
        print "entering the texts are done!"
    else:
        getting_text(raw_input("Enter the text or write done to finish entering "))
        user_input.append(entered_text)

getting_text(raw_input("Enter the first text "))
print user_input

Am I misunderstanding something here, because the print function prints c,b,a instead of a,b,c (the order I entered the input is a,b,c)

like image 817
firko Avatar asked Feb 12 '16 09:02

firko


People also ask

How is a list ordered in Python?

Lists Are OrderedThe order in which you specify the elements when you define a list is an innate characteristic of that list and is maintained for that list's lifetime. (You will see a Python data type that is not ordered in the next tutorial on dictionaries.)

Can a list be sorted Python?

Python lists have a built-in list.sort() method that modifies the list in-place. There is also a sorted() built-in function that builds a new sorted list from an iterable.

How do you sort a list from highest to lowest in Python?

Descending (or decreasing) order is the opposite of ascending order - elements are arranged from highest to lowest value. To sort list items in descending order, you need to use the optional reverse parameter with the sort() method, and set its value to True .

How do you put a list in ascending order in Python?

Python list sort() function can be used to sort a List in ascending, descending, or user-defined order. In each case, the time complexity is O(nlogn) in Python.


1 Answers

Ok, this is what's happening.

When your text isn't "done", you've programmed it so that you immediately call the function again (i.e, recursively call it). Notice how you've actually set it to append an item to the list AFTER you do the getting_text(raw_input("Enter the text or write done to finish entering ")) line.

So basically, when you add your variables, it's going to add all of the variables AFTER it's done with the recursive function.

Hence, when you type a, then it calls the function again (hasn't inputted anything to the list yet). Then you type b, then c. When you type done, the recursive bit is finished. NOW, it does user_input.append(.... HOWEVER, the order is reversed because it deals with c first since that was the latest thing.

This can be shown when you print the list inside the function:

>>> def getting_text(entered_text):
...     print user_input
...     if entered_text == "done":
...         print "entering the texts are done!"
...     else:
...             getting_text(raw_input("Enter the text or write done to finish entering "))
...             user_input.append(entered_text)
... 
>>> 
>>> getting_text(raw_input("Enter the first text "))
Enter the first text a
[]
Enter the text or write done to finish entering b
[]
Enter the text or write done to finish entering c
[]
Enter the text or write done to finish entering done
[]
entering the texts are done!
>>> user_input
['c', 'b', 'a']

Note the print statement line 2.


So how do you fix this? Simple: append to the list before you recursively call.

>>> user_input = []
>>> def getting_text(entered_text):
...     if entered_text == "done":
...         print "entering the texts are done!"
...     else:
...             user_input.append(entered_text)
...             getting_text(raw_input("Enter the text or write done to finish entering "))
... 
>>> user_input = []
>>> getting_text(raw_input("Enter the first text "))
Enter the first text a
Enter the text or write done to finish entering b
Enter the text or write done to finish entering c
Enter the text or write done to finish entering done
entering the texts are done!
>>> user_input
['a', 'b', 'c']
like image 198
TerryA Avatar answered Sep 29 '22 09:09

TerryA