List in Python
List in Python
PYTHON
By Harshita Gupta
WHAT IS LIST?
List is mutable sequence of values seperated by commas, enclosed in square
brackets.
Elements of lists consists of different data types, i.e., strings, int, float, and even
another list can be the element of the main list.
e.g:- list1=[].
Here the list1 is the example of Empty list, as it’s name suggest, this type of list
doesn’t have any Element in it.
Some examples regarding list having heterogeneous data type consists in it:-
e.g:- list1=[]
Output:- []
Nested lists:- A nested list have list inside a list, or in other words, A list
containing another list as element
e.g:- mylist=[10,20,30,[‘my’, ‘love’, ‘apple’], ‘mango’]
print(mylist)
Output:- [10,20,30,[‘my’, ‘love’, ‘apple’], ‘mango’]
Long lists:- As for the long list, they contain many elements, which make a list
long,...in other words long lists consists of many elements of same or different data
types.
e.g:- mylist=[20,30,30,[‘my’, ‘love’, ‘apple’], ‘mango’, ‘lol’, ‘rocket’]
output:- [10,20,30,[‘my’, ‘love’, ‘apple’], ‘mango’, ‘lol’, ‘rocket’]
CREATING LIST FROM A
SEQUENCE
my_list=list(‘Computer’)
print(my_list)
Output:-
[‘C’, ‘o’, ‘m’, ‘p’, ‘u’, ‘t’, ‘e’, ‘r’]
CREATING LIST BY USER
INPUTS METHOD
list1=list(input(‘Enter the values:’))
print(list1)
Enter the values: Hi Python
Output:-
[‘H’, ‘i’, ‘’, ‘P’, ‘y’, ‘t’, ‘h’, ‘o’, ‘n’]
ACCESSING LIST ELEMENTS
A list consists of any collection of items of items which are stored according
to its index.
We can access any desired value by it’s indexing value.
Indexing value are of two types:-
print(list[3])
print(list[-1])
Output:-
love
logical
SLICING
Indexing and slicing are two inter-related operations in lists. Slicing is done
through indexing.
Slicing is an operation in which we can slice a particular range from that
sequence.
To select a specific elements from the list
BUILT-IN FUNCTIONS
Append:- list.append(elem)
Insert:- list.insert(index,elem)
Extend:- list.extend(list2)
Index:- list.index(elem)
Remove:- list.remove(elem)
Sort:-list.sort()
Reverse:- list.reverse()
APPEND()
The Append() method adds a single item to the end of the list.
It doesn’t create a new list, it just modifies the original list.
Syntax:
list.append(item)
EXTEND()
This method adds one list at the end of another list.
In implying, all the items of a list are added to an already created list.
Syntax:
list.extend(list2)
INSERT()
The insert() can be used to insert an element at a specific index.
Syntax: list_name.insert(insert_number, value)
REVERSE()
The reverse() reverses the order of the elements in a list._
It doesn’t create any new list.
INDEX()
Index() returns the index of first matched item from the list.
It returns the first occurrence of an item for which the index position is to
be searched for in the list.
UPDATING LIST
Lists are mutable, new value can be assigned to existing value. Using phone‘=‘.
List[index]=<new value>
LEN()
Len() function returns length of the list , i.e., number of elements in a list.
SORT()
This sorts the items of the list, by default in increasing order.
It doesn’t create a new list.
CLEAR()
Clear() method removes all items from the list.
Clear() method just empties the given list, and doesn’t return any value.
DELETION OPERATION
Methods to delete item from a list.
If index is known, pop() or del statement.
If element is known but not the index, remove() is used.
To remove more than one element, del() with list slice can be used.
POP()
It removes the elements from a specified index and also returns the
element which was removed.
Syntax:
List.pop(index)
DEL STATEMENT
Del statement removes the specified element from the list but does not
return the deleted value.
REMOVE()
The remove() function is used when the value to be deleted is known but
not the index of the element.
THANK YOU