Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

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?

like image 586
samoz Avatar asked Dec 04 '12 00:12

samoz


People also ask

Does Python set preserve insertion order?

Sets use a different algorithm that isn't as amendable to retaining insertion order. Set-to-set operations lose their flexibility and optimizations if order is required. Set mathematics are defined in terms of unordered sets.

Is a Python set always sorted?

Sets are an unordered and unindexed collection having no duplicate elements. Sets are one of the four built-in data types available in Python and are written using curly brackets. Given that sets are unordered, it is not possible to sort the values of a set.

Are lists immutable in Python?

Lists and Tuples in Python Integers, floats, strings, and (as you'll learn later in this course) tuples are all immutable. Once one of these objects is created, it can't be modified, unless you reassign the object to a new value. The list is a data type that is mutable.

Which of the following is true about list in Python?

Explanation: Elements of lists are stored in contagious memory location is the true regarding lists in Python.


2 Answers

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

like image 160
sge Avatar answered Oct 13 '22 08:10

sge


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" 
like image 38
ApproachingDarknessFish Avatar answered Oct 13 '22 08:10

ApproachingDarknessFish