0% found this document useful (0 votes)
14 views6 pages

FPP List

The document provides an overview of lists in programming, detailing their characteristics such as mutability, ability to store heterogeneous data, and allowance for duplicates. It explains how to create lists, access elements, add and remove items, and perform operations like sorting and reversing. Additionally, it covers concepts of aliasing and cloning of list objects, along with differences between certain list methods.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views6 pages

FPP List

The document provides an overview of lists in programming, detailing their characteristics such as mutability, ability to store heterogeneous data, and allowance for duplicates. It explains how to create lists, access elements, add and remove items, and perform operations like sorting and reversing. Additionally, it covers concepts of aliasing and cloning of list objects, along with differences between certain list methods.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Lists : A collection of similar or different types of data items.

If we want to represent a group of individual objects as a single entity where


insertion order is preserved and duplicates are allowed, then we should go for List.
- A list can store heterogeneous data.
- Items in a list are mutable (changeable).
- A list can have duplicate items.
- It is a dynamic data structure/data type.
Creation of a list :
A list is created by placing items inside square brackets ([ ]) separated by comma.
E.g. A list of 10 natural numbers.
list = [1,2,3,4,5,6,7,8,9,10]
- We can also create an empty list . E.g. list = [ ]
- With split() function :
E.g. list = input().split()
- With dynamic input (eval())
E.g. list = eval(input())
#input - [10,’j’,5,4]
Print(type(list))
Single dimensional list – A single d-list is a list where elements are
listed one after the other. E.g. list = [5,10,15,20,25]
Multi dimensional list – A multi-d list or nested list is a list containing
another list,
E.g. list = [‘Python’, [10,45,23], 20]
Accessing elements of a list :
We can access characters of a string by using the following ways :
1. By using index
2. By using slice operator
 Adding items to a list :
append() – a built in method used to add an item at the end of the list.
Syntax – list_name.append(value)
insert() – used to add an item at a specific position.
Syntax – list_name.insert(index, value)
extend() – used to add all items of one list in another list.
Syntax – list1.extend(list2)
 Removing items from a list :
remove() – removes the specified item.
Syntax – list.remove(value)
pop() – removes an item at the specified index.
Syntax – list.pop(index)
pop() not only removes the item, but returns that item too, so that it can be
stored and used in next problem.
If the index of that item is not specified then the last list item will be deleted.
Using the del keyword : removes the item on a specified index, if the index is not
specified deletes the whole list.
E.g. del list[1] , del list
clear() : Only the items of the list will be deleted.
Syntax – list.clear()
E.g. list = [1,2,3,4,5]
list.clear() #op – [ ]
 To get information about list :
Len() ,count(), index(), max(), min()
Ordering elements of a list :
reverse() : used to reverse the order of elements in a list. It does not return a new list
but instead modifies the original list.
Syntax – list_name.reverse()
Sort() : used to sort the elements of a list. By default the order of sorting is in
ascending order.
Syntax – list_name.sort(key= None , reverse = False) (Both the parameters- are
optional).
Note: To use sort() function, compulsory list should contain only homogeneous elements.
Mathematical operators for list : ‘+’ for concatenation and ‘*’ for repetition.
Checking membership : in, not in
 Aliasing and cloning of list objects :
Aliasing : The process of giving another reference variable to the existing list is
called aliasing.
E.g. x =[ 10,20,30]
Y=x
Print(id(x))
Print(id(y)) # both statements will print the same address.
The problem in this approach is by using one reference variable if we are
changing content, then those changes will be reflected to the other reference
variable.
To overcome this problem we should go for cloning.
Cloning : The process of creating exactly duplicate independent object is called
cloning.
We can implement cloning by using slice operator or by using copy() function
• Difference between remove() and pop().
• Difference between append() and insert().

You might also like