Practical 5 List
Practical 5 List
Date:
Python Lists are just like dynamically sized arrays, declared in other languages (vector in C++ and
ArrayList in Java). In simple language, a list is a collection of things, enclosed in [ ] and separated by
commas.
The list is a sequence data type which is used to store the collection of data. Tuples and String are other
types of sequence data types.
Example of list in Python
Here we are creating Python List using [].
print(Var)
Output:
["Geeks", "for", "Geeks"]
Lists are the simplest containers that are an integral part of the Python language. Lists need not be
homogeneous always which makes it the most powerful tool in Python. A single list may contain
DataTypes like Integers, Strings, as well as Objects. Lists are mutable, and hence, they can be altered even
after their creation.
Creating a List in Python
Lists in Python can be created by just placing the sequence inside the square brackets[]. Unlike Sets, a list
doesn’t need a built-in function for its creation of a list.
Note: Unlike Sets, the list may contain mutable elements.
Python3
# Creation of List
# Creating a List
List = []
print(List)
print(List)
# using index
print(List[0])
print(List[2])
Output
Blank List:
[]
List of numbers:
[10, 20, 14]
List Items:
Geeks
Geeks
A list may contain duplicate values with their distinct positions and hence, multiple distinct or duplicate
values can be passed as a sequence at the time of list creation.
Python3
List = [1, 2, 4, 4, 3, 3, 3, 6, 5]
print(List)
print(List)
Output
List with the use of Numbers:
[1, 2, 4, 4, 3, 3, 3, 6, 5]
print(List[0])
print(List[2])
Output
Accessing a element from the list
Geeks
Geeks
Example 2: Accessing elements from a multi-dimensional list
Python3
# index number
print(List[0][1])
print(List[1][0])
Output
Accessing a element from a Multi-Dimensional list
For
Geeks
Negative indexing
In Python, negative sequence indexes represent positions from the end of the array. Instead of having to
compute the offset as in List[len(List)-3], it is enough to just write List[-3]. Negative indexing means
beginning from the end, -1 refers to the last item, -2 refers to the second-last item, etc.
Python3
# negative indexing
print(List[-1])
print(List[-3])
Output
Accessing element using negative indexing
Geeks
For
Getting the size of Python list
Python len() is used to get the length of the list.
Python3
# Creating a List
List1 = []
print(len(List1))
print(len(List2))
Output
0
3
Taking Input of a Python List
We can take the input of a list of elements as string, integer, float, etc. But the default one is a string.
Example 2:
Python
elements:").strip().split()))[:n]
Output:
Enter the size of list : 4
Enter the integer elements: 6 3 9 10
The list is: [6, 3, 9, 10]
To know more see this.
Adding Elements to a Python List
Elements can be added to the List by using the built-in append() function. Only one element at a time can
be added to the list by using the append() method, for the addition of multiple elements with the append()
method, loops are used. Tuples can also be added to the list with the use of the append method because
tuples are immutable. Unlike Sets, Lists can also be added to the existing list with the use of the append()
method.
Python3
Output
Initial blank List:
[]
append() method only works for the addition of elements at the end of the List, for the addition of elements
at the desired position, insert() method is used. Unlike append() which takes only one argument, the
insert() method requires two arguments(position, value).
)
Other than append() and insert() methods, there’s one more method for the Addition of elements, extend(),
this method is used to add multiple elements at the same time at the end of the list.
Note: append() and extend() methods can only add elements at the end.
Python3
# Python program to demonstrate
# Creating a List
List = [1, 2, 3, 4]
print(List)
print(List)
Output
Initial List:
[1, 2, 3, 4]
List after performing Extend Operation:
[1, 2, 3, 4, 8, 'Geeks', 'Always']
Reversing a List
Method 1: A list can be reversed by using the reverse() method in Python .
Python3
# Reversing a list
mylist.reverse()
print(mylist)
Output
['Python', 'Geek', 5, 4, 3, 2, 1]
Method 2: Using the reversed() function:
The reversed() function returns a reverse iterator, which can be converted to a list using the list() function.
Python3
my_list = [1, 2, 3, 4, 5]
reversed_list = list(reversed(my_list))
print(reversed_list)
Output
[5, 4, 3, 2, 1]
Removing Elements from the List
Elements can be removed from the List by using the built-in remove() function but an Error arises if the
element doesn’t exist in the list. Remove() method only removes one element at a time, to remove a range
of elements, the iterator is used. The remove() method removes the specified item.
Note: Remove method in List will only remove the first occurrence of the searched element.
Example 1:
Python3
# Creating a List
List = [1, 2, 3, 4, 5, 6,
print(List)
# Removing elements from List
List.remove(5)
List.remove(6)
print(List)
Output
Initial List:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
# Creating a List
List = [1, 2, 3, 4, 5, 6,
List.remove(i)
print(List)
Output
List after Removing a range of elements:
[5, 6, 7, 8, 9, 10, 11, 12]
pop() function can also be used to remove and return an element from the list, but by default it removes
only the last element of the list, to remove an element from a specific position of the List, the index of the
element is passed as an argument to the pop() method.
Python3
List = [1, 2, 3, 4, 5]
List.pop()
print(List)
# Removing element at a
List.pop(2)
print(List)
Output
List after popping an element:
[1, 2, 3, 4]
# Creating a List
Sliced_List = List[3:8]
print(Sliced_List)
Sliced_List = List[5:]
print(Sliced_List)
Sliced_List = List[:]
print(Sliced_List)
Output
Initial List:
['G', 'E', 'E', 'K', 'S', 'F', 'O', 'R', 'G', 'E', 'E', 'K', 'S']
Python3
# Creating a List
print(List)
Sliced_List = List[:-6]
print(Sliced_List)
Sliced_List = List[-6:-1]
Sliced_List = List[::-1]
print(Sliced_List)
Output
Initial List:
['G', 'E', 'E', 'K', 'S', 'F', 'O', 'R', 'G', 'E', 'E', 'K', 'S']
PRACTI CAL – 2
A) Create a list and apply methods (append, extend, remove, reverse), arrange the created list
in ascending and descending order.
list2 = [15, 7]
list1.extend(list2) print("After
extend : ",list1)
List1 =
[1,2,3,4,["python","java","c++",[10,20,30]],5,6,7,["apple","bana na","orange"]]
#orange
print("List1[8][2] : ",List1[8][2])
#python
print("List1[4][0] : ",List1[4][0])
#print 5 times
print("5 times : \n",List1*5)
#indexing
print("Element at index 3 : ",tup[3])