Subscribe to DSC Newsletter

comparative study note of Python and Java 3 - Sequence type

3. Sequence Type


This time, we compare the sequence types of the two languages. By the terminology sequence type, we mean the data structure that is kind of a sequence of data.

In Java, we have: String, array, list(arraylist and linkedlist), set;

In Python, we have: Str, tuple, list, set;

Yes, String is one of the sequence type, because it is a sequence  of char, isn’t it? About the Sting type, we only need to know its implementation is the same in both languages, and that’s it.

I:array vs tuple

They are rarely be used in the real world, but they are the very fundamental block.  The shot cut to understand them is to notice 3 things:

1, array is dynamic in elements value, while tuple is static in elements value.

You can assign a new value to the elements of an array whenever you want, code like:

int[] myArray = {1,2,3};




is valid. But in Python, once a tuple is built, then the value of the elements is fixed like constants. You can’t change it. Code like:

myTuple = (1,2,3)

myTuple[0] = 2

is not valid.

2, array and tuple are both static in size .

Static in size means once an array or a tuple is well built, you can’t change its size, you can’t append a new element to it.

3, array is static typed, tuple is dynamic typed.

Array elements are bounded by the type you state when you declare the array. Like:

int[] arrayName;

then each of the elements of arrayName must be integer type.

In Python, there is no type bound on the tuple object. You can build a tuple like this:

myTuple = (1,’a’,2.3,”hello “)


II: list

List is a very important sequence type in both languages because of its dynamic feature. In java, the mainly difference of list from array is that the size of a list is adjustable. You can adjust the size of a list when necessary. And in Python, the mainly difference of list from tuple is that you can change the value of elements whenever you want.  

1, List in Java

In java, List is not a primitive type. It is an interface of three classes: ArrayList, LinkedList and Vector. The List is a resizable array(It is actually developed base on array). For more details(for example, difference between ArrayList and LinkedList) you can refer to the following link:

2, list in Python

In Python list is a primitive type, thus it’s very convenient to use. The following link will lead you to the site that shows the detail.

The summary is, the list type in both languages has the same features, adjustable size, mutable elements and the elements could be accessed by index. And again, in Java, the elements of list must be of the same type (primitive type or predefined class) while in Python there is no type bound for the elements of list.

III: set

Set is a mathematical concept. It is a collection of distinct elements, with particular set operators: intersection and union. In Python, you can build up a collection with the same features by the type set. The following is a simple tutorial for the type set in Python:

However, in Java, the type set is quite different.  The same thing is, duplicating elements is not allowed. But the mathematical set operations are not well supported(compared to Python). Please refer to the following link for more details:

It seems that the type set in Java focus more on the performance of accessing and mutating elements. And Python focus more on set calculations than Java.

IV: looping for sequence type

In java, we generally use for loop like this:

for (int i =0; condition on i;i++)


It also supply a foreach loop for sequence iteration:

for(type  element : sequence object)


The loop will take some actions on each element of the sequence object. If you read ‘:’ as ‘in’, then the syntax is analog to the for loop in Python.

There is one thing about looping in Python is worth to notice. When using range() and xrange() methods for looping, the performance is different:

import time


for i in range(1000): pass


for i in xrange(1000):pass


print t2-t1

print t3-t2   




Obviously, using  xrange() is better. Also, by range(size), you create a list of a determined size. Conversely, xrange(end number) is a generator  which generate a number from 0 until the end number, thus xrange() costs less memory.  Conclusion, xrange() is better for looping in Python.


When studying Java and Python, always keep in mind the distinction of type bound, namely the static-typed and dynamic-typed feature. In java, all objects are strictly restrained in type; In Python, all objects are always free of type restrict.

Views: 1030

Tags: array, difference, each, for, java, list, loop, python, range(), set, More…tuple, xrange()


You need to be a member of AnalyticBridge to add comments!

Join AnalyticBridge

Comment by Yuecai Zhu on November 15, 2013 at 10:58am

Thanks for your complememt Davide. When I check the Java api, I skipped these two methods because of the boolean return type.

When apply c.retainAll(b) and c.addAll(b), the set c will be modified to be the result(the intersection or the union of the original sets).

I will do the correction to the article. Thanks again Davide!

Comment by Davide Imperati on November 15, 2013 at 9:59am

Nice comparison, it is a good guide to the choice of a programming language.

In Java, set operations can be performed using addAll(.) for the union and retainAll(.) for the intersection

e.g. c <- a intersection b is:

Set<...> c = new HashSet<...>(a);


e.g. c <- a union b becomes
Set<...> c = new HashSet<...>(a);


On Data Science Central

© 2021   TechTarget, Inc.   Powered by

Badges  |  Report an Issue  |  Privacy Policy  |  Terms of Service