0% found this document useful (0 votes)
2 views25 pages

Python List

This document provides an introduction to Python lists, covering their properties, creation, and methods for accessing and modifying list items. It explains concepts such as concatenation, indexing, and built-in functions, along with examples of list manipulation. Additionally, it discusses the concept of aliasing in Python, highlighting how changes to aliased objects affect each other.

Uploaded by

bingewat9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views25 pages

Python List

This document provides an introduction to Python lists, covering their properties, creation, and methods for accessing and modifying list items. It explains concepts such as concatenation, indexing, and built-in functions, along with examples of list manipulation. Additionally, it discusses the concept of aliasing in Python, highlighting how changes to aliased objects affect each other.

Uploaded by

bingewat9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Python List

Div_F
List Introduction

• List: an object that contains multiple data items


• List is an ordered set of data items.
• Element: An item in a list
• Format: list = [item1, item2, etc.]
• List constants are surrounded by square brackets and the elements in
the list are separated by commas
• Can hold items of different types i.e. integers, strings, other lists
Properties of List:

• Heterogeneous (any data type!)


• Contiguous
• Have random access to any element
• Ordered (numbered from 0 to n-1)
• Number of elements can change very easily (use method .append)
• Python lists are mutable sequences of arbitrary objects
List Creation
List constants are surrounded by square brackets and the elements in the list
are separated by commas.
#empty list
my_list = []

# list of integers
my_list = [1, 2, 3]

# list with mixed datatypes


my_list = [1, "Hello", 2.5]
Accessing and editing list items

• Accessing Element from list


list1 = [3,5,9,6]
print(list1)
print(list1[0])
print(list1[1])
• Editing Lists
As well as reading individual elements we can write to individual elements too:
list2 = [3,5,9,6]
print(list2)
list2[1] = 2
print(list2)
Concatenating Lists

• Concatenate: join two things • >>> a = [1, 2, 3]


together • >>> b = [4, 5, 6]
• The + operator can be used to • >>> c = a + b
concatenate two lists
– Cannot concatenate a list with
• >>> print( c )
another data type, such as a • [1, 2, 3, 4, 5, 6]
number
• >>> print( a )
• The += augmented assignment
operator can also be used to • [1, 2, 3]
concatenate lists
repetition operator

• The repetition operator makes multiple copies of a list and joins them all together.
• Ex.
numbers = [0, 1, 2] * 3
print(numbers)

Output:
[0, 1, 2, 0, 1, 2, 0, 1, 2]
Finding Items in Lists with the in Operator

• You can use the in operator to • >>> some = [1, 9, 21, 10, 16]
determine whether an item is • >>> 9 in some
contained in a list
General format: item in list • True
Returns True if the item is in the list, • >>> 15 in some
or False if it is not in the list • False
• Similarly you can use the not in • >>> 20 not in some
operator to determine whether an
item is not in a list • True
Lists Are Mutable

• Mutable sequence: the items in the sequence can be changed


Lists are mutable, and so their elements can be changed
• An expression such as
list[1] = new_value can be used to assign a new value to a list element
Indexing in List

Index: a number specifying the position of an element in a list


• Enables access to individual element in list
• Index of first element in the list is 0, second element is 1, and n’th
• element is n-1
• Negative indexes identify positions relative to the end of the list
• The index -1 identifies the last element, -2 identifies the next to last element,
etc.
List Methods and Useful Built-in Functions
• len function: It returns the length of a sequence such as a list
• Example: size = len(my_list)
• It returns the number of elements in the list, so the index of last
element is len(list)-1
Example:
My_list = [23,45,67]
print(len(My_list))
Output = 3
And index of last element i.e 67 is len(My_list)-1 i.e. 3-1 =2
List Methods and Useful Built-in Functions
• append(item): used to add items to a list – item is appended to the end of the
existing list
list1 = [1, 3, 5]
list1.append(7)
print(list1)
Output: [1, 3, 5, 7]
List Methods and Useful Built-in Functions
• index(item): used to determine where an item is located in a list It
returns the index of the first element in the list containing item
list=[1,2,3,4]
X =list.index(3)
print(X)
Output: 2
• count(item): It returns number of times the item occurs in the list.
Ex. list=[‘a’,‘i’,’b’, ‘i’ ]
print(list.count(‘i’))
Output: 2
List Methods and Useful Built-in Functions
• insert(index, item): It is used to insert item at position index in the list
list1 = [1, 9]
list1.insert(1,3)
print(odd)
Output: [1, 3, 9]

• sort(): It is used to sort the elements of the list in ascending order


Num = [5,7,3,2,6] Sorted list in descending order
Num.sort() Num = [5,7,3,2,6]
Print(Num) Num.sort(reverse = True)
Output: [2,3,5,6,7] Print(Num)
Output: [7,6,5,3,2]
List Methods and Useful Built-in Functions
• reverse(): reverses the order of the elements in the list
list=[23,13,3,41]
list.reverse()
print(list)
Output: [41,3,13,23]

• extend(seq): It takes single argument (a list) and adds it to the end.


List = [‘a’, ‘b’, 2]
List1 = [‘c’,3]
List.extend(List1)
Print(List)
Output: [‘a’, ‘b’, 2, ‘c’,3]
List Methods and Useful Built-in Functions
• remove(item): It removes the first occurrence of item in the list
my_list = ['p',‘y',‘t',’h’,‘o',‘n‘]
my_list.remove('p')
print(my_list)
Output: [‘y',‘t',’h’,‘o',‘n‘]
• del statement: removes an element from a specific index in a list
General format: del list[i]
my_list = ['p',‘y',‘t',’h’,‘o',‘n‘]
del my_list[2]
print(my_list)
Output: ['p', ‘y', ‘h', ‘o', ‘n’]
List Methods and Useful Built-in Functions

• min() and max() functions: built-in functions that returns the item
that has the lowest or highest value in a sequence
• sum(): It returns the sum of all the elements in the list.
• The sequence is passed as an argument
• Ex.
List1 = [1,6,3,4]
print(max(list1))
Output: 6
Example

thislist = ["apple", "banana", "cherry"]


for x in thislist:
print(x)
Output:
apple
banana
cherry
Example
list1 = [10,11,”a”,3.5] list1 = [10,11,”a”,3.5]
for i in list1: for i in range(4):
print(list[i])
print(i)

Output: Output:
10 10
11
11 a
a 3.5
3.5
Example
• Write a program that asks the user to enter a list of integers. Do the following:
(a) Print the total number of items in the list.
(b) Print the last item in the list.
(c) Print the list in reverse order.
(d) Print Yes if the list contains a 5 and No otherwise.
(e) Print the number of fives in the list.
(f) Remove the first and last items from the list, sort the remaining items, and print
the result.
(g) Print how many integers in the list are less than 5.
Example
• Write a program that generates a list L of 50 random numbers between 1 and 100.
from random import randint
L = []
for i in range(50):
L.append(randint(1,100))
The program takes a list and puts the even and odd
elements in it into two separate lists.
for i in range(1,n+1):
b=int(input("Enter element:")) Output:
Enter number of elements:3
a.append(b)
Enter element:23
even=[]
Enter element:44
odd=[]
Enter element:99
for j in a: The even list [44]
if(j%2==0): The odd list [23, 99]
even.append(j)
else:
odd.append(j)
print("The even list",even)
print("The odd list",odd)
Examples

• Ask the user to enter a list containing numbers between 1 and 12. Then replace all
of the entries in the list that are greater than 10 with 10.
• Take two numbers from user in a and b and create a list of all even numbers
between a and b.
• Write a program to find duplicate in list.
Aliasing
• If a refers to an object and you assign b = a, then both variables refer
to the same object:
• >>> a = [1, 2, 3]
• >>> b = a
• >>> b is a
True
• An object with more than one reference has more than one name, so
we say that the object is aliased.
Aliasing
• If the aliased object is mutable, changes made with one alias affect
the other:
• >>> b[0] = 17
• >>> print(a)
[17, 2, 3]

You might also like