Skip to content

List and Tuple

LIST:
It is a collection of data items stored into a single variable. List can be created using pair of [ ]. List data items can be of any datatype.

🐍
filename.py
#Example
L1=[1,3,5]
print(type(L1))
L1=["Raj",32,True]
print(type(L1))
L1=[] #empty list
print(type(L1))
L1=list()
print(type(L1))

List can also be have +ve and -ve indexing those can be used to access elements of list individually or slicing.

List is mutable means individual list element can be changed using its index value.

🐍
filename.py
#Example
L1=[1,3,5]
print(L1)
L1[2]=10
print(L1)

We can create list using an existing list variable.

🐍
filename.py
#Example
L1=[1,3,[2,4]]
L2=[2,4,L1]
print(L1)
print(L2)

Length of the list can be returned using len() function

🐍
filename.py
#Example
L1=[1,3,2,4]
print(len(L1))

Adding new items into list
We can new items at
1. the end of the list
2. the specified index

Β 

The append() method:
used to add the specified element at the end of the list

🐍
filename.py
#Example
lst=[1,3,2,4]
print(lst)
lst.append(12)
print(lst)

The insert() method:
used to add a value at specified index position

🐍
filename.py
#Example
lst=[2,4,6,8]
print("lst[0]",lst[0])
print("lst[1]",lst[1])
print("lst[2]",lst[2])
print("lst[3]",lst[3])
lst.insert(2,5)
print()
print("lst[0]",lst[0])
print("lst[1]",lst[1])
print("lst[2]",lst[2])
print("lst[3]",lst[3])
print("lst[4]",lst[4])

Removing data items from list
We can do this by the following ways:
1. del command
2. remove method
3. pop method
4. clear method

Β 

Del command:
it can be used to remove one item at specific index position or entire list

🐍
filename.py
#Example
lst=[2,4,6,8]
print(lst)
del lst[2]
print(lst)
del lst
print(lst) #Gives Error

remove method:
it is used to remove a list item based on its value.

🐍
filename.py
#Example
fruits=["apple","banana","grapes","orange"]
print(fruits)
fruits.remove("grapes")
print(fruits)

pop method;
it is used to remove last element of the list or element at a spefic index.

🐍
filename.py
#Example
fruits=["apple","banana","grapes","orange"]
print(fruits)
fruits.pop()
print(fruits)
fruits.pop(1)
print(fruits)

Clear method:
it is used to remove all elements from the list so that it becomes empty.

🐍
filename.py
#Example
fruits=["apple","banana","grapes","orange"]
print(fruits)
fruits.clear()
print(fruits)

Copy List into other list:
A List variable cannot be directly assigned into other List variable.

🐍
filename.py
#Example
fruits1=["apple","banana","grapes","orange"]
fruits2=fruits1
fruits1[2]="mango"
print(fruits1)
print(fruits2)

There are two methods to duplicate a list:
1. copy method
2. list method

🐍
filename.py
#Example of copy method
fruits1=["apple","banana","grapes","orange"]
fruits2=fruits1.copy()
fruits1[2]="mango"
print(fruits1)
print(fruits2)

#Example of list method
fruits1=["apple","banana","grapes","orange"]
fruits2=list(fruits1)
fruits1[2]="mango"
print(fruits1)
print(fruits2)

We can concatenate two lists to create a new list

🐍
filename.py
#Example of list concatenation
fruits1=["apple","banana","grapes","orange"]
fruits2=["Kiwi","guava","papaya"]
fruits=fruits1+fruits2
print(fruits)
fruits3=fruits2*2
print(fruits3)

TUPLE:
Tuple is also one of the python’s internal data structures. The differences between lists and tuple are:
1. Lists are created using square brackets while tuples are created with parenthesis.
2. Lists are mutable while Tuples are immutable

🐍
filename.py
#Exampe
tup1 = ('physics', 'chemistry', 1997, 2000);
tup2 = (1, 2, 3, 4, 5 );
tup3 = "a", "b", "c", "d"
tup1 = (); #Empty tuple
tup1 = (50,); #tuple with single element must be created with additional comma

#NOT a tuple
thistuple = ("apple")
print(type(thistuple))

Tuples are also having positive and negative indices to access elements

🐍
filename.py
#Example code access tuple elements using indices
tup1 = ('physics', 'chemistry', 1997, 2000);
tup2 = (1, 2, 3, 4, 5, 6, 7 );
print ("tup1[0]: ", tup1[0]);
print ("tup2[1:5]: ", tup2[1:5]);

Negative indexing means beginning from the end, -1 refers to the last item, -2 refers to the second last item etc.

🐍
filename.py
#Example
#Print the last item of the tuple
thistuple = ("apple", "banana", "cherry")
print(thistuple[-1])

Length of the tuple can get using len() function.

Β 

Tuple is immutable
Once a tuple is created, you cannot change its values. Tuples are unchangeable or immutable.

🐍
filename.py
#Example:
tup1 = (12, 34.56);
tup2 = ('abc', 'xyz');

# Following action is not valid for tuples
# tup1[0] = 100;

# So let's create a new tuple as follows
tup3 = tup1 + tup2;
print (tup3);

Convert Tuple as List
You can convert the tuple into a list, change the list, and convert the list back into a tuple.

🐍
filename.py
#Example:
x = ("apple", "banana", "cherry")
#x[1]="kiwi" this is not possible
y = list(x) #converting tuple into a list
y[1] = "kiwi"
x = tuple(y) #converting a list into a tuple
print(x)

Delete Tuple Elements:
Removal of individual tuple elements is not possible. But we can delete entire tuple with β€˜del’ command.

🐍
filename.py
#Example:
tup = ('physics', 'chemistry', 1997, 2000);
print (tup);
del tup; #but del tup[0] is not possible
print ("After deleting tup : ");
print (tup);

Concatenation and extending tuples:

🐍
filename.py
tup1=(1, 2, 3) + (4, 5, 6) #returns (1, 2, 3, 4, 5, 6)
tup2=('Hi!',) * 4 #returns ('Hi!', 'Hi!', 'Hi!', 'Hi!')
print(tup1)
print(tup2)