• Lists and Tuples are called compound data types and are one of the key types of data structures in Python.


  • Tuples are an ordered sequence. Tuples are expressed as comma separated elements within parentheses. There are values inside the parentheses.


  • 
    # Create your first tuple
    
    tuple1 = ("disco",10,1.2 )
    tuple1
     
    
  • 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.


  • 
    # Print the type of the tuple you created
    
    type(tuple1)
     
    
  • The first element can be accessed by the name of the tuple, followed by a square bracket with the index number - 0


  • 
    # Print the variable on each index
    
    print(tuple1[0])
    print(tuple1[1])
    print(tuple1[2])
    
    # Print the type of value on each index
    
    print(type(tuple1[0]))
    print(type(tuple1[1]))
    print(type(tuple1[2]))
     
    
  • In Python, we can use negative index.


  • 
    # Use negative index to get the value of the last element
    
    tuple1[-1]
    tuple1[-2]
    tuple1[-3]
     
    
  • We can concatenate or combine tuples by adding them


  • 
    # Concatenate two tuples
    
    tuple2 = tuple1 + ("hard rock", 10)
    tuple2
     
    
  • If we would like multiple elements from a tuple, we could also slice tuples.


  • 
    # Slice from index 0 to index 2
    
    tuple2[0:3]
    
    # Slice from index 3 to index 4
    
    tuple2[3:5]
     
    
  • we can use the len command to obtain the length of the tuple. If there are five elements, the result is five.


  • 
    # Get the length of tuple
    
    len(tuple2)
     
    
  • Tuples are immutable, which means we can't change them.


  • Let's say we want to change the element at index 2. Because tuples are immutable, we can't.


  • 
    # A sample tuple
    
    Ratings = (0, 9, 6, 5, 10, 8, 9, 6, 2)
     
    
  • 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.


  • 
    # Sort the tuple
    
    RatingsSorted = sorted(Ratings)
    RatingsSorted
     
    
  • 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.


  • 
    # Create a nest tuple
    
    NestedT =(1, 2, ("pop", "rock") ,(3,4),("disco",(1,2)))
    
    # Print element on each index
    
    print("Element 0 of Tuple: ", NestedT[0])
    print("Element 1 of Tuple: ", NestedT[1])
    print("Element 2 of Tuple: ", NestedT[2])
    print("Element 3 of Tuple: ", NestedT[3])
    print("Element 4 of Tuple: ", NestedT[4])
    
    
    
    # Print element on each index, including nest indexes
    
    print("Element 2, 0 of Tuple: ",   NestedT[2][0])
    print("Element 2, 1 of Tuple: ",   NestedT[2][1])
    print("Element 3, 0 of Tuple: ",   NestedT[3][0])
    print("Element 3, 1 of Tuple: ",   NestedT[3][1])
    print("Element 4, 0 of Tuple: ",   NestedT[4][0])
    print("Element 4, 1 of Tuple: ",   NestedT[4][1])
    
    
    # Print the first element in the second nested tuples
    
    NestedT[4][1][0]
    # Print the second element in the second nested tuples
    
    NestedT[4][1][1]
     
    
  • A list is represented with square brackets. In may respects, lists are like tuples. One key difference is they are mutable.


  • 
    # Create a list
    
    L = ["Michael Jackson", 10.1, 1982]
    L
     
    
  • 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.


  • ListsIndex
    
    # Print the elements on each index
    
    print('the same element using negative and positive indexing:\n Postive:',L[0],
    '\n Negative:' , L[-3]  )
    print('the same element using negative and positive indexing:\n Postive:',L[1],
    '\n Negative:' , L[-2]  )
    print('the same element using negative and positive indexing:\n Postive:',L[2],
    '\n Negative:' , L[-1]  )
     
    
  • The first element can be accessed by the name of the list followed by a square bracket with the index number, in this case, 0.


  • ListsNeg
    
    # Sample List
    
    ["Michael Jackson", 10.1, 1982, [1, 2], ("A", 1)]
    
    
    # Sample List
    
    L = ["Michael Jackson", 10.1,1982,"MJ",1]
    L
     
    
  • We can also perform slicing in lists.


  • perform slicing in lists
    
    # List slicing
    
    L[3:5]
    
    # Use extend to add elements to list
    
    L = [ "Michael Jackson", 10.2]
    L.extend(['pop', 10])
    L
     
    
  • The last index is one larger than the length of the list. The index conventions for list and tuples are identical.


  • We can concatenate or combine list by adding them.


  • 
    # Use append to add elements to list
    
    L = [ "Michael Jackson", 10.2]
    L.append(['pop', 10])
    L
    
    # Use extend to add elements to list
    
    L = [ "Michael Jackson", 10.2]
    L.extend(['pop', 10])
    L
     
    
  • Lists are mutable, therefore, we can change them. For example, we apply the method extends by adding a dot followed by the name of the method, then parentheses. The argument inside the parentheses is a new list that we are going to concatenate to the original list.


  • 
    # Use append to add elements to list
    
    L.append(['a','b'])
    L
     
    
  • In this case, instead of creating a new list L1, the original list L is modified by adding two new elements.


  • Another similar method is append. If we apply append instead of extend, we add one element to the


  • Every time we apply a method, the list changes.


  • As lists are mutable, we can change them.


  • 
    # Change the element based on the index
    
    A = ["disco", 10, 1.2]
    print('Before change:', A)
    A[0] = 'hard rock'
    print('After change:', A)
     
    
  • We can delete an element of a list using the Del command. We simply indicate the list item we could like to remove as an argument.


  • 
    # Delete the element based on the index
    
    print('Before change:', A)
    del(A[0])
    print('After change:', A)
     
    
  • We can convert a string to a list using split. We can use the split function to separate strings on a specific character known as a delimiter. We simply pass the delimiter we would like to split on as an argument, in this case, a comma. The result is a list, each element corresponds to a set of characters that have been separated by a comma.


  • 
    # Split the string, default is by space
    
    'hard rock'.split()
    
    # Split the string by comma
    
    'A,B,C,D'.split(',') 
    
  • When we set one variable B equal to A, both A and B are referencing the same list. Multiple names referring to the same object is known as aliasing.


  • referencing the same list
    
    # Copy (copy by reference) the list A
    
    A = ["hard rock", 10, 1.2]
    B = A
    print('A:', A)
    print('B:', B)
    
     
    
    referencing the same list
    
    # Examine the copy by reference
    
    print('B[0]:', B[0])
    A[0] = "banana"
    print('B[0]:', B[0])
    
     
    
  • A and B are referencing the same list, therefore, if we change A, list B also changes.


  • Clone (clone by value)
    
    # Clone (clone by value) the list A
    
    B = A[:]
    B
    
    print('B[0]:', B[0])
    A[0] = "hard rock"
    print('B[0]:', B[0])
     
    
  • If we check the first element of B after changing list A, we get banana instead of hard rock.


  • You can clone list A by using the following syntax, variable A references one list. Variable B references a new copy or clone of the original list. Now, if you change A, B will not change.


  • We can get more info on list, tuples, and many other objects in Python using the help command.


  • Simply pass in the list, tuple, or any other Python object.



  1. Explanation :

    the index 0 corresponds to the first element of a list or tuple


  1. Explanation :

    the index 2 corresponds to the third element in the tuple, which contains another list.


  1. Explanation :

    A[2] corresponds to the third nested list; we then access the only element of the list using the index 0 i.e. A[2][0].


  1. Explanation :

    split returns a list of the words in the string, separated by the delimiter string, in this case, a comma.


  1. Explanation :

    append-only adds one element.


  1. Explanation :

    lists are mutable tuples are not