Lists & Tuples

These are called compound data types and are one of the key types of data structures in Python.Tuples, are an ordered sequence. Here is a tuple ratings.
Tuples are expressed as comma separated elements within parentheses, so these are values inside the parentheses.

In Python, there are different types,strings, integer, float. They can all be contained in a tuple. But the type of the variable is tuple. Each element of a tuple can be accessed via an index.
The first element can be accessed by the name of the tuple, followed by a square bracket with the index number-in this case, 0.We can also access the last element. In Python, we can use negative index. The relationship is as follows: The corresponding values are shown here. We can concatenate or combine tuples by adding them. The result is the following with the following index.
If we would like multiple elements from a tuple, we could also slice tuples. For example, if we want the first three elements we use the following command:
The last index is one larger than the index you want.
Similarly, if we want the last two elements, we use the following command.
Notice how the last index is one larger than the length of the tuple.

We can use the len command to obtain the length of the tuple. As there are 8 elements. The result is 8.

Tuples are immutable, which means we can’t change them.
Each variable does not contain a tuple but references the same immutable tuple object.

Let’s say we want to change the element at index 2. Because tuples are immutable, we can’t. Therefore, ratings 2 will not be effected by a change in rating because the tuple is immutable-i.e, we can’t change it. We can assign a different tuple to the ratings variable. The variable ratings now references another tuple. As a consequence of immutability,if we would like to manipulate a tuple, we must create a new tuple instead. For example, if we would like to sort a tuple, we use the function sorted. The input is the original tuple. The output is a new sorted tuple.


A tuple can contain other tuples as well as other complex data types. This is called nesting. We can access these elements using the standard indexing methods. If we select an index with the tuple, the same index convention applies. As such, we can then access values in the tuple. For example, we could access the second element.
rating[1]//accessing the value of the second element
for example:
tuple1[1:1]//to access the value 56
We can apply this indexing directly to the tuple variable tuple1.

We can visualize this nesting as a tree. The tuple has indexes. If we consider indexes with other tuples, we see the tuple at index 1 contains a tuple with two elements. We can access those two indexes. We can access the elements in those tuples as well. We can continue the process.

Lists are also an ordered sequence.

A list is represented with square brackets. In many respects, lists are like tuples. One key difference is they are mutable. Lists can contain strings, floats, integers. We can nest other lists. We also nest tuples and other data structures. The same indexing conventions apply for nesting. Like tuples, each element of a list can be accessed via an index. We can concatenate or combine list by adding them. The result is a new list.Lists are mutable, therefore, we can change them.

# Create your first tuple
tuple1 = (“disco”,10,1.2 )
(‘disco’, 10, 1.2)

# Print the type of the tuple you created

# Print the variable on each index

# Print the type of value on each index
<class ‘str’>
<class ‘int’>
<class ‘float’>

# Use negative index to get the value of the last element

# Use negative index to get the value of the second last element

# Use negative index to get the value of the third last element

# Concatenate two tuples
tuple2 = tuple1 + (“hard rock”, 10)
(‘disco’, 10, 1.2, ‘hard rock’, 10)

# Slice from index 0 to index 2
(‘disco’, 10, 1.2)

# Slice from index 3 to index 4
(‘hard rock’, 10)

# Get the length of tuple