List
List
Value – This refers to the value stored by the object. For example
We can't change the type of object but we can change the value of the
object.
For example, we set variable a as a list, now we can't change variable from
One, those objects which can change their internal state (the
data/content inside those objects) i.e. they can be changed with the help of
objects).
Mutable-Definition
Python, ‘mutable’ is the ability of objects to change their values. These are
which those objects are stored remains the same. This is explained in
Lists
Sets
Dictionaries
Immutable-Definition
initialize them. There are no methods and functions which can be used
objects.
This means that after you create the object and assign some value
Strings
Tuples
Frozen Sets
Example 1:
str1="Kalpana"
str1[0]="k"
print(str1)
Output:
Example 2:
a=1
print(id(a)) # address=9788992
a=2
print(id(a)) # address=9789024
Note:
Why are these values different if you’re using variable name a in both
cases?
Explanation:
value 2, the original object with value 1 is still in memory, but you can’t
Mutable Immutable
The objects can be modified after the Objects can not be modified after
Classes are not made final for the Classes are made final for the
Example: Lists, Dicts, Sets, User-Defined Example: int, float, bool, string,
ease to the coder to pass around the object in the program without any
resulting in variability.
deduces that a read-only data is being shared among the threads, which
objects can be achieved in such a way that one creates a new object
Limitations
Over time, different conclusions get introduced with it, as we read that
Immutables is Thread safe, and no matter which thread reads their values,
They get the right values, but The immutables are found to be immune
objects is not enough to achieve this. One has to also guard against
The size of a list can also change during execution, as elements are added or
A list can grow and shrink during execution of the program. Hence it is also
length data.
brackets where items or elements need not all have the same type.Lists are
A list in Python is used to store the sequence of various types of data. Python
lists are mutable type its mean we can modify its element after it created.
Example 1:
colors=["red","green","blue"]
print(colors)
Output:
Like this we can print all the elements of a list in one go.
Example 2:
colors=["red","green","blue"]
print(colors[0])
print(colors[1])
print(colors[2])
Output:
red
green
blue
starting from 0.
Here
print(colors[0]) ## red
print(colors[2]) ## green
print(len(colors)) ## 3
Example 3:
Characteristics of Lists
o The lists are ordered (all elements have definite order/sequence, any
number of times).
o List elements are changeable (we can add, delete item from the
list).
We can also print elements of array using loops. In loop, in every iteration
one element of list is selected and printed, then another element is picked
and printed and so on till all the elements of list are complete.
for e in colors:
print(e)
Output:
green
red
blue
yellow
while i<len(colors):
print(colors[i])
i=i+1
Output:
green
red
blue
yellow
Negative Indexing
Usually the indexes start from left side and with numeric value zero. In
addition to this, in Python we have the concept of negative index also which
print(colors[0])
print(colors[1])
print(colors[2])
print(colors[-1])
print(colors[-2])
print(colors[-3])
Output:
green
red
blue
blue
red
green
Note:You can use negative indexing as your advantage when you want to
Whenever we display a list using print command, all the elements of the list
Example :
Output:
Note:
function:
Example:
colors=["red","green","blue"]
print(len(colors))
Output:
One can find the data type of a list with type() function. lists are defined as
objects with the data type 'list'. A list can contain any element of any data
type.
Example:
colors=["red","green","blue"]
print(type(colors))
mynumbers=[1,2,3]
print(type(mynumbers))
mybool=["TRUE","FALSE","FALSE"]
print(type(mybool))
Output:
<class 'list'>
<class 'list'>
<class 'list'>
Concept: A list can contain multiple elements with different data types.
Example:
print(mylist)
Output:
Example:
Lst1=["a","b","c","d","e"]
print(Lst1)
print(Lst1)
print(Lst1)
print(Lst1)
Output:
Example:
Lst1=list(“List Topic”)
print(Lst1)
Output:
['L', 'i', 's', 't', ' ', 'T', 'o', 'p', 'i', 'c']
Example:
a=[1,3,5]
b=[6,8,9]
c=[0,2,4]
d=[a,b,c]
print(d)
print(d[0][1])
print(d[2][1])
Output:
Example:
X=[1,2,3]
Y=[10,20,X]
Print(Y)
print(Y[1])
print(Y[2])
Output:
20
[1, 2, 3]
Example:
X=[1,2,3]
Y=[10,20,*X]
print(Y)
print(Y[1])
print(Y[2])
Output:
[10, 20, 1, 2, 3]
20
List Methods
Python has a set of built-in methods that you can use on lists.
Metho Description
d
extend( Add the elements of a list (or any iterable), to the end of the
) current list
index() Returns the index of the first element with the specified value
Append Items
Appending an element in the list means to add an element at the end. To
add an item to the end of the list, use the append() method:
Example 1:
colors.append("silver")
print(colors)
Output:
Example 2:
#Appending two element at a time. Both the elements will be added one
after another.
colors.append("silver")
colors.append("golden")
print(colors)
Output:
The clear method is used to clear all the elements from the list.
The clear() method empties the list.The list still remains, but it has no
content.
Example:
colors = ["green", "red", "blue","pink","yellow"]
print(colors)
colors.clear()
print(colors)
Output:
[]
Copy a List
Copy a list means to copy elements of one list into another. If the contents/
elements of list are copied by = operator, then new list created will just be a
copy of original list. Whatever changes you make in original list will
Example:
colors2=colors1
print(colors2)
Output:
Example:
print(colors2)
Output:
Count()
This method is used to count the number of times a element has occurred in
the list.
Example 1:
print(colors.count("red"))
Output:
Example 2:
print(colors.count("black"))
Output:
Extend()
Extend means to increase the total number of elements in the list by adding
another list at the end. To append elements from another list to the current
Example:
colors1.extend(colors2)
print(colors1)
Output:
Index()
The index() method returns the position at the first occurrence of the
specified value. If any element is occurring more than once, then first
occurrence is considered.
Example 1:
print(colors.index("blue"))
Output:
Example 2:
print(colors.index("black"))
Output:
Error
Insert()
This method is used to insert an element at any particular location in the list.
To insert a new list item, without replacing any of the existing values, we can
use the insert() method. The insert() method inserts an item at the specified
index.
Example 1:
print(colors)
colors.insert(2,"black")
print(colors)
Output:
Example 2:
print(colors)
colors.insert("black")
print(colors)
Output:
Error
Example 3:
print(colors)
colors.insert(8,"black")
print(colors)
Output:
Pop()
The pop() method removes the element by specifying specified index. The
element present at the specified index in popped out from the list.
Example 1:
popped=colors.pop()
print(colors)
print(popped)
Output:
['green', 'red']
blue
Example 2:
popped=colors.pop(0)
print(colors)
print(popped)
Output:
['red', 'blue']
green
Example 3:
popped=colors.pop(6)
print(colors)
print(popped)
Output:
Error
Remove()
Example 1:
colors.remove("blue")
print(colors)
Output:
['green', 'red']
Example 2:
colors.remove("pink")
print(colors)
Output:
Error
Pop() Vs Remove()
Pop() Remove()
Reverse()
element at index 0 will go to index n-1 and element at index n-1 will go to
index 0.
Example:
colors.reverse()
print(colors)
Output:
Sort()
Sometimes one may need to arrange all the elements of list in ascending or
descending order. List objects have a sort() method that will sort the list
alphanumerically in ascending or descending order. By default the order is
ascending.
colors.sort()
print(colors)
Output:
mynum=[34,67,12,87,43]
mynum.sort()
print(mynum)
Output:
colors.sort(reverse=1)
print(colors)
Output:
mynum.sort(reverse=1)
print(mynum)
Output:
Concept:
In case of small and capital letters, we get unexpected answer while sorting.
colors.sort()
print(colors)
Output:
colors.sort(key=str.lower)
print(colors)
Output:
the in keyword.
Example 1:
colors = ["green", "red", "blue","pink","yellow"]
if "red" in colors:
Example 2:
if "black" in colors:
One may need to change the value of a single item or multiple elements
within a specific range. First you need to define a list with the new values,
and refer to the range of index numbers where you want to insert the new
values.
Example 1:
print(colors)
colors[1] = "black"
print(colors)
Output:
Example 2:
print(colors)
colors[2:4] = ["black","saffron"]
print(colors)
Output:
Example 3:
print(colors)
colors[2:4] = ["black","saffron","grey"]
print(colors)
Output:
Example 4:You can use negative indexing as your advantage when you
print(colors)
colors[2:4] = ["black"]
print(colors)
Output:
del()
This method is used to remove an element from a specific index number.
Example 1:
print(colors)
del colors[0]
print(colors)
Output:
['red', 'blue']
Example 2:
print(colors)
del colors
print(colors)
Output:
Example 3:
print(colors)
del colors[2:4]
print(colors)
Output:
['green', 'red', 'blue', 'magenta', 'pink']
Python offers a very simple operator to join two lists, which is +. With just
Example:
colors2=["black","white","grey"]
colors3=colors1+colors2
print(colors3)
Output:
Python provides the following built-in functions, which can be used with the
lists.
SN Function Description Example
lists. versions.
the list. 72
12
print(type(s))
<class list>