Data

Array

Python

 

Defining List

Python 2.7
list1 = [1, 2, 3]

List is a container for a sequence of items.

Lists are similar to Arrays in other Programming Languages.

The elements inside List don't need to have the same type.

 

Defining Tuple

Python 2.7
tuple1 = (1, 2, 3)

tuple2 = (1,)

tuple3 = ()

Tuples are immutable lists.

Once Tuples are defined, they cannot be changed.

If you have only one element inside your Tuple Literal, you should insert a comma after the element.

 

Length

Python 2.7
list1  = [1, 2, 3]
tuple1 = (1, 2, 3)

length1 = len(list1)    # 3
length2 = len(tuple1)   # 3

Function len() returns the length of a List or Tuple.

 

Get Element at index

Python 2.7
animals1 = ["Cat", "Dog", "Cow"]
animals2 = ("Cat", "Dog", "Cow")

cat1 = animals1[0];    # Cat
cat2 = animals2[0];    # Cat

List and Tuple elements can be accessed by their index.

In Python, Indexes start from 0.

 

Set Element at index

Python 2.7
animals1 = ["Cat", "Dog", "Cow"]
animals2 = ("Cat", "Dog", "Cow")

animals1[0] = "Lion"
animals2 = animals2[:0] + ("Lion",) + animals2[1:]

Here we are setting an element at index 0 to a value Lion.

Since Tuples are immutable, in order to set a new value at a specific index, you need to create a new Tuple.

 

Append Element

Python 2.7
list1  = [1, 2, 3]
tuple1 = (1, 2, 3)

list1.append(4)
tuple1 = tuple1 + (4,)

You can append an element to the end of a List using append() method.

Since Tuples are immutable, to append a value to it, you need to create a new Tuple.

 

Find Element

Python 2.7
animals1 = ["Cat", "Dog", "Cow"]
animals2 = ("Cat", "Dog", "Cow")

index1 = animals1.index("Dog")    # 1
index2 = animals2.index("Dog")    # 1

The index() method searches for the index of the first occurrence of an element inside a List or Tuple.

If the element does not exist, it will throw an Error.

 

Get Sub-Sequence

Python 2.7
animals1 = ["Cat", "Dog", "Cow"]
animals2 = ("Cat", "Dog", "Cow")

sub10 = animals1[:]      # ["Cat", "Dog", "Cow"]
sub11 = animals1[2:]     # ["Cow"]
sub12 = animals1[:2]     # ["Cat", Dog]
sub13 = animals1[2:3]    # ["Cow"]

sub20 = animals2[:]      # ("Cat", "Dog", "Cow")
sub21 = animals2[2:]     # ("Cow",)
sub22 = animals2[:2]     # ("Cat", Dog)
sub23 = animals2[2:3]    # ("Cow",)

The bracket [:] operator gets starting index (inclusive) and ending index (exclusive) and returns the SubList or SubTuple for those parameters.

If you don't mention the starting or ending index, Python will assume that you're looking for the extreme ends.

 

Sort Sequence

Python 2.7
list1  = [2, 4, 3, 1]
tuple1 = (2, 4, 3, 1)

list2  = sorted(list1)   # [1,2,3,4]
tuple2 = sorted(tuple1)  # (1,2,3,4)

Function sorted() orders a List or Tuple.

There is also a sort() method on Lists that can be used. This method sorts Lists in-place, while sorted() returns a sorted sequence.

In order to sort the sequence in reverse, use the Reversed parameter:

list1  = [2, 4, 3, 1]
tuple1 = (2, 4, 3, 1)

list2  = sorted(list1, reverse=True)
tuple2 = sorted(tuple1, reverse=True)
 

Convert to String

Python 2.7
animals1 = ["Cat", "Dog", "Cow"]
animals2 = ("Cat", "Dog", "Cow")

str1 = ', '.join(animals1)   # Cat, Dog, Cow
str2 = ', '.join(animals2)   # Cat, Dog, Cow

The join() method concatenate Sequence elements and returns a String.

It gets a string, and use that as a delimiter between elements in the string.

If the sequence elements are not a string, you can convert each element to string and then join them:

list1  = [2, 4, 3, 1]
tuple1 = (2, 4, 3, 1)

str1 = ', '.join(map(str, list1))  # 2, 4, 3, 1
str2 = ', '.join(map(str, tuple1)) # 2, 4, 3, 1