Is a Python list guaranteed to have its elements stay in the order they are inserted in?

The Question :

337 people think this question is useful

If I have the following Python code

>>> x = []
>>> x = x + [1]
>>> x = x + [2]
>>> x = x + [3]
>>> x
[1, 2, 3]



Will x be guaranteed to always be [1,2,3], or are other orderings of the interim elements possible?

• A list, by definition, is an ordered sequence of items, accessible by their indices.

507 people think this answer is useful

Yes, the order of elements in a python list is persistent.

86 people think this answer is useful

In short, yes, the order is preserved. In long:

In general the following definitions will always apply to objects like lists:

A list is a collection of elements that can contain duplicate elements and has a defined order that generally does not change unless explicitly made to do so. stacks and queues are both types of lists that provide specific (often limited) behavior for adding and removing elements (stacks being LIFO, queues being FIFO). Lists are practical representations of, well, lists of things. A string can be thought of as a list of characters, as the order is important ("abc" != "bca") and duplicates in the content of the string are certainly permitted ("aaa" can exist and != "a").

A set is a collection of elements that cannot contain duplicates and has a non-definite order that may or may not change over time. Sets do not represent lists of things so much as they describe the extent of a certain selection of things. The internal structure of set, how its elements are stored relative to each other, is usually not meant to convey useful information. In some implementations, sets are always internally sorted; in others the ordering is simply undefined (usually depending on a hash function).

Collection is a generic term referring to any object used to store a (usually variable) number of other objects. Both lists and sets are a type of collection. Tuples and Arrays are normally not considered to be collections. Some languages consider maps (containers that describe associations between different objects) to be a type of collection as well.

This naming scheme holds true for all programming languages that I know of, including Python, C++, Java, C#, and Lisp (in which lists not keeping their order would be particularly catastrophic). If anyone knows of any where this is not the case, please just say so and I’ll edit my answer. Note that specific implementations may use other names for these objects, such as vector in C++ and flex in ALGOL 68 (both lists; flex is technically just a re-sizable array).

If there is any confusion left in your case due to the specifics of how the + sign works here, just know that order is important for lists and unless there is very good reason to believe otherwise you can pretty much always safely assume that list operations preserve order. In this case, the + sign behaves much like it does for strings (which are really just lists of characters anyway): it takes the content of a list and places it behind the content of another.

If we have

list1 = [0, 1, 2, 3, 4]
list2 = [5, 6, 7, 8, 9]



Then

list1 + list2



Is the same as

[0, 1, 2, 3, 4] + [5, 6, 7, 8, 9]



Which evaluates to

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]



Much like

"abdcde" + "fghijk"



Produces

"abdcdefghijk"



5 people think this answer is useful

I suppose one thing that may be concerning you is whether or not the entries could change, so that the 2 becomes a different number, for instance. You can put your mind at ease here, because in Python, integers are immutable, meaning they cannot change after they are created.

Not everything in Python is immutable, though. For example, lists are mutable—they can change after being created. So for example, if you had a list of lists

>>> a = [[1], [2], [3]]
>>> a[0].append(7)
>>> a
[[1, 7], [2], [3]]



Here, I changed the first entry of a (I added 7 to it). One could imagine shuffling things around, and getting unexpected things here if you are not careful (and indeed, this does happen to everyone when they start programming in Python in some way or another; just search this site for “modifying a list while looping through it” to see dozens of examples).

It’s also worth pointing out that x = x + [a] and x.append(a) are not the same thing. The second one mutates x, and the first one creates a new list and assigns it to x. To see the difference, try setting y = x before adding anything to x and trying each one, and look at the difference the two make to y.

5 people think this answer is useful

You are confusing ‘sets’ and ‘lists’. A set does not guarantee order, but lists do.

Sets are declared using curly brackets: {}. In contrast, lists are declared using square brackets: [].

mySet = {a, b, c, c}



Does not guarantee order, but list does:

myList = [a, b, c]



0 people think this answer is useful

aList=[1,2,3]

i=0

for item in aList:

if i<2:

aList.remove(item)

i+=1



aList

[2]

The moral is when modifying a list in a loop driven by the list, takes two steps:

aList=[1,2,3]
i=0
for item in aList:
if i<2:
aList[i]="del"
i+=1

aList

['del', 'del', 3]
for i in range(2):
del aList[0]

aList
[3]



0 people think this answer is useful

Yes lists and tuples are always ordered while dictionaries are not

Tags: