List Methods
List Methods
PROGRAMMING IS AN ART
Lists are one of 4 built-in data types in Python used to store collections of data,
the other 3 are Tuple, Set and Dictionary, all with different qualities and usage.
Output:-
[‘apple’, ‘banana’, ‘mango’]
List items :-
ordered, changeable, and allow duplicate values
Indexed, the first item has index [0], second item has index [1] etc
Ordered :-
the items have a defined order, and that order will not change
add new items to a list, the new items will be placed at the end of
the list
Changeable :-
changeable, meaning that we can change, add, and remove items in
a list after it has been created
• Allow Duplicates :- List are indexed , list have can
items with the same value.
Input :-
thislist = [“apple”, “banana”, “cherry”, “apple”, ”cherry”]
print(thislist)
Output :-
[‘apple’, ‘banana’, ‘cherry’, ‘apple’, ‘cherry’]
List Length :- To determine how many items a list
has, use the len() function.
Input :-
thislist = [“apple”, “banana”, “cherry”]
print(len(thislist))
Output :-
3
• List Item – Data Types
list item can be of any data type
String, int and boolean data types
- A list can contain different data type:
• type()
From Python's perspective, lists are defined as objects with the
data type 'list'
<class ‘list’>
append () :- Adds an element at the end of the list.
Input :-
tops = [“urvi”, “manthan”]
tops.append(“tirth”)
print(tops)
Output :-
[‘urvi’, ‘manthan’, ‘tirth’]
clear() :- Remove all the elements from the list.
Input :-
fruits = [“apple”, “banana”, “cherry”]
fruits.clear()
print(fruits)
Output :-
[]
• copy() :- The copy() method returns a copy of the specified
list.
Input :-
citys = [“Ahmedabad”, “Vadodara”, “Surat”]
m = citys.copy()
print(m)
Output :-
[‘Ahmedabad’, ‘Vadodara’, ‘Surat’]
• count() :- The count() method returns the number of
elements with the specified value.
Input :-
phones = [“iphone”, “Vivo”, “Oppo”]
m = phones.count(iphone)
print(m)
Output :-
1
extend() :- The extend() method adds the specified list
elements to the end of the current list.
Input :-
cars = [“ford”,” “toyota”, “tata”]
bikes = [“hero”, “honda”, “yamaha”]
cars.extend(bikes)
print(cars)
Output :-
[‘ford’, ‘toyota’, ‘tata’, ‘hero’, ‘honda’, ‘Yamaha’]
index() :- The index() method returns the position at the
first occurrence of the specified value.
Input :-
ford_cars = [“endeavor”, “mustang”, “raptor”]
m = ford_cars.index(“mustang”)
print(m)
Output :-
1
• insert() :- The insert() methods inserts the specified value at
the specified position.
Input :-
languages = [ “jawa”, “c++”, “c”]
languages.insert(1, “python”)
print(languages)
Output :-
[‘jawa’, ‘python’, ‘c++’, ‘c’]
pop() :- The pop() methods removes the element at the
specified position.
Input :-
phones = [ “vivo”, “oneplus”, “iphone”]
phones.pop(1)
print(phones)
Output :-
[‘vivo’, ‘iphone’]
remove() :- The remove() methods removes the first
occurrence of the element with the specified position.
Input :-
phones = [ “vivo”, “oneplus”, “iphone”]
phones.remove(oneplus)
print(phones)
Output :-
[‘vivo’, ‘iphone’]
reverse() :- The reverse() methods reverse the sorting order
of the element.
Input :-
phones = [ “vivo”, “oneplus”, “iphone”]
phones.reverse()
print(phones)
Output :-
[‘iphone’, ‘oneplus’, ‘vivo’ ]
sort() :- The sort() methods sorts the list ascending by
default.
You can also make function to decide the sorting criteria(s).
Input :-
cars = [ “Ford”, “Audi”, “ BMW”]
phone.sort()
print(cars)
Output :-
[‘Audi’, ‘BMW’, ‘Ford’ ]
THANK YOU