You are on page 1of 19

PYTHON PROGRAMMING

(KNC- 302)
UNIT 3

By- Dr. Nidhi Saxena


Asst. Professor
CS Dept.
What Is a List?
A list is a linear data structure , meaning that its elements have a linear ordering.
That is, there
is a fi rst element, a second element, and so on.

Python List Type


A list in Python is a mutable, linear data structure of variable length, allowing mixed-
type
elements. Mutable means that the contents of the list may be altered. Lists in Python
use zerobased
indexing. Thus, all lists have index values 0 ... n-1, where n is the number of elements
in the list. Lists are denoted by a comma-separated list of elements within square
brackets as
shown below,
[1, 2, 3] ['one', 'two', 'three'] ['apples', 50, True]
Internal working of list in Python
Python lists are internally represented as arrays. The idea used is similar
to implementation of vectors in C++ or ArrayList in Java. The costly operations are
inserting and deleting items near the beginning (as everything has to be moved).
Insert at the end also becomes costly if preallocated space becomes full.

We can access each element of a list in python by their assigned index. In python starting index of
list sequence is 0 and ending index is (if N elements are there) N-1.

Viewing the elements of List in Python :


Individual items of a list can be accessed through their indexes as done in below
code segment.

list1 = [1, 2, 3, 4] Output-

print(list1[1:3]) [2,3]

# accessing through negative index


print(list1[-1]) 4
Slicing 2D List (List inside List)
l=[3,6,4,2,7]
print(l[:4])
[3, 6, 4, 2] a = [[1,2,3],[4,5,6],[7,8,9]]
5
print(a[1][1])
2
print(l[3:]) print(a[0][1])
8
[2, 7] print(a[2][1])
6
print(a[1][2])
print(l[-3])
4
print(l[:])
[3, 6, 4, 2, 7] Joining 2 lists
a = [1,2,3]
print(l[::2]) b = [4,5,6]
[3, 4, 7] c = a+b
print(c)
print(l[::-1])
[7, 2, 4, 6, 3]
Assigning and Accessing data:
For creating a list we need to specify the elements inside square brackets ‘[]’ and then
give it a name. Whenever you want to access the list elements then use this list name
and index of element you want to show.

Each element in list is assigned an index in positive indexing we have index from 0 to
end of the list and in negative indexing we have index from -N(if elemts are N) till -1.
As shown in above examples the work of accessing elements is manual. We can also
access or assign elements through loops.
# assigning elements to list
list1 =[]
for i in range(0, 11):
list1.append(i)

# accessing elements from a list


for i in range(0, 11):
print(list1[i])
Updating list:
We can update already assigned elements to the list and also can append one
element at a time to your list.Even you can extend your list by adding another list to
current list.
The above task can be performed as follows.

list1 =[1, 2, 3, 4] Output:-

[1, 2, 5, 4
[1, 2, 5, 4, 6]
# updating [1, 2, 5, 4, 6, 1, 2, 3]
list1[2]= 5
print(list1)

# appending
list1.append(6)
print(list1)

# extending
list1.extend([1, 2, 3])
print(list1)
Deleting elements of list :
We can delete elements in lists by making use of del function. In this you need to
specify the position of element that is the index of the element and that element will
be deleted from the list and index will be updated.

list1 = [1, 2, 3, 4, 5]
print(list1)

# deleting element
del list1[2]
print(list1)
map

map is another useful function available in Python. Suppose you have a list of characters and you
want to convert all of them to int in one go, then you can use 'map'. Let's see an example of map:

a = ['1','2','3','4']
b = list(map(int,a))
print(b)

index is a function which gives us the index of an element of a list. It will be clear
from the example given below

Print a.index(‘2’)
Assigning and Copying Lists

... List1 = [10, 20, 30, 40]


... List2 = list1
... list1[0]= 5
... list1
[5, 20, 30, 40] #change made in list1
... list2
[5, 20, 30, 40] #change in list1 causes a change in list2

This issue does not apply to strings and tuples, since they are immutable and therefore
cannot be modified.

When needed, a copy of a list can be made as given below,


list2 = list(list1)
In this case, we get the following results,
... List1 = [10, 20, 30, 40]
... list2 = list(list1)
... list1[0] = 5
... List1
[5, 20, 30, 40] change made in list1
... list2
[10, 20, 30, 40] change in list1 does NOT cause any change in list2
List comprehensions in Python provide a concise means of generating a
more varied set of sequences than those that can be generated by the
range function.
List comprehensions in Python can be used to generate more varied sequences.
Example list comprehensions are given

... temperatures = [ 88, 94, 97, 89, 101, 98, 102, 95, 100]
... [t for t in temperatures if t >=100]
???
... [(t -32) * 5/9 for t in temperatures]
Tuple
In Python, a tuple is similar to List except that the objects in tuple are immutable
which means we cannot change the elements of a tuple once assigned. On the other
hand, we can change the elements of a list.

Tuple vs List
1. The elements of a list are mutable whereas the elements of a tuple are
immutable.
2. When we do not want to change the data over time, the tuple is a preferred
data type whereas when we need to change the data in future, list would be a wise
option.
3. Iterating over the elements of a tuple is faster compared to iterating over a list.
4. Elements of a tuple are enclosed in parenthesis whereas the elements of list are
enclosed in square bracket.
Examples
my_data2 = (1, 2.8, "Hello World") or

My_data2=(1,2.8,’hello world’
print(my_data2)

# tuple of string and list


my_data3 = ("Book", [1, 2, 3])
print(my_data3)

# tuples inside another tuple


# nested tuple
my_data4 = ((2, 3, 4), (1, 2, "hi"))

# empty tuple
my_data = ()
print(my_data4)

# a tuple with single data item


my_data = (99,)

# accessing third element


print(my_data3[2])

my_data = (1, 2, "Kevin", 8.9)


# accessing last element # prints 8.9
print(my_data[-1])
Accessing elements from nested tuples

my_data = (1, "Steve", (11, 22, 33))


# prints 'v'
print(my_data[1][3])
# prints 22
print(my_data[2][1])

Operations that can be performed on tuple in Python


Changing the elements of a tuple
We cannot change the elements of a tuple because elements of tuple are immutable.
However we can change the elements of nested items that are mutable. For example,
in the following code, we are changing the element of the list which is present inside
the tuple. List items are mutable that’s why it is allowed.

my_data = (1, [9, 8, 7], "World")


print(my_data)
my_data[1][2] = 99
print(my_data)

# deleting entire tuple is possible


del my_data
Slicing operation in tuples l=(6,9,2,3,4,5)
print(l[3:6])
(3, 4, 5)
Membership Test in Tuples print(l[::-1])
in: Checks whether an element (5, 4, 3, 2, 9, 6)
exists in the specified tuple. # false
print(2 in my_data)
not in: Checks whether an # false
element does not exist in the print(88 not in my_data)

specified tuple. # tuple of fruits


my_tuple = ("Apple", "Orange", "Grapes", "Banana")
Iterating a tuple # iterating over tuple elements
for fruit in my_tuple: print(fruit)
Slicing operation in tuples
print(l[3:6])
l=(6,9,2,3,4,5)
(3, 4, 5)
print(l[::-1])
(5, 4, 3, 2, 9, 6)
Membership Test in Tuples
in: Checks whether an element exists in the specified tuple.
not in: Checks whether an element does not exist in the specified tuple.
# false
print(2 in my_data)
# false
print(88 not in my_data)

Iterating a tuple

# tuple of fruits
my_tuple = ("Apple", "Orange", "Grapes", "Banana")
# iterating over tuple elements
for fruit in my_tuple: print(fruit)

You might also like