List in python
* LIST IN PYTHON *
List is a collection which is ordered and changeable. Allows duplicate members.
means you can add same data in list as well as change the list data ,size(length), etc..
Example
Create a List:thislist = ["apple", "banana", "cherry"]
print(thislist)
OUTPUT :-
["apple", "banana", "cherry"]
Access Items
You access the list items by referring to the index number:Example
Print the second item of the list:thislist = ["apple", "banana", "cherry"]
print(thislist[1])
OUITPUT:-
"apple"
Change Item Value
To change the value of a specific item, refer to the index number:Example
Change the second item:thislist = ["apple", "banana", "cherry"]
thislist[1] = "blackcurrant"
print(thislist)
OUTPUT:-
["apple", "blackcurrant", "cherry"]
Loop Through a List
You can loop through the list items by using afor
loop:Example
Print all items in the list, one by one:
thislist = ["apple", "banana", "cherry"]
for x in thislist:
print(x)
OUTPUT :-
"apple"
"banana"
"cherry"
Check if Item Exists
To determine if a specified item is present in a list use the in
keyword:
Example
Check if "apple" is present in the list:
thislist = ["apple", "banana", "cherry"]
if "apple" in thislist:
print("Yes, 'apple' is in the fruits list")
OUTPUT :-
"Yes, 'apple' is in the fruits list"
List Length
To determine how many items a list has, use the
len()
method:
Example
Print the number of items in the list:
thislist = ["apple", "banana", "cherry"]
print(len(thislist))
OUTPUT :-
3
Add Items
To add an item to the end of the list, use the append()
method:
Example
Using the append() method to append an item:
thislist = ["apple", "banana", "cherry"]
thislist.append("orange")
print(thislist)
OUTPUT :-
["apple", "banana", "cherry","orange"]
or
To add an item at the specified index, use the insert() method:
Example
Insert an item as the second position:
thislist = ["apple", "banana", "cherry"]
thislist.insert(1, "orange")
print(thislist)
OUTPUT :-
["apple","orange", "banana", "cherry"]
Remove Item
There are several methods to remove items from a list:
Example
The remove()
method removes the specified item:
thislist = ["apple", "banana", "cherry"]
thislist.remove("banana")
print(thislist)
OUTPUT:-
["apple", "cherry"]
Example
The pop()
method removes the specified
index, (or the last item if index is not specified):
thislist = ["apple", "banana", "cherry"]
thislist.pop()
print(thislist)
OUTPUT:-
["apple", "banana"]
Example
The del
keyword removes the specified
index:
thislist = ["apple", "banana", "cherry"]
del
thislist[0]
print(thislist)
OUTPUT:-
["banana", "cherry"]
Example
The del
keyword can also delete the list
completely:
thislist = ["apple", "banana", "cherry"]
del
thislist
OUTPUT:-
None
Example
The clear()
method empties the list:
thislist = ["apple", "banana", "cherry"]
thislist.clear()
print(thislist)
OUTPUT:-
[]Copy a List
You cannot copy a list simply by typinglist2 =
list1
, because: list2
will only be a
reference to list1
, and changes made in
list1
will automatically also be made in
list2
.There are ways to make a copy, one way is to use the built-in List method
copy()
.Example
Make a copy of a list with thecopy()
method:
thislist = ["apple", "banana", "cherry"]
mylist
= thislist.copy()
print(mylist)
OUTPUT:-
["apple", "banana", "cherry"]
Another way to make a copy is to use the built-in method
list()
.Example
Make a copy of a list with thelist()
method:
thislist = ["apple", "banana", "cherry"]
mylist
= list(thislist)
print(mylist)
OUTPUT:-
["apple", "banana", "cherry"]
The list() Constructor
It is also possible to use the list() constructor to make a new list.Example
Using the list() constructor to make a List:thislist = list(("apple", "banana", "cherry")) # note the double round-brackets
print(thislist)
OUTPUT:-
["apple", "banana", "cherry"]List Methods
Python has a set of built-in methods that you can use on lists.Method | Description |
---|---|
append() | Adds an element at the end of the list |
clear() | Removes all the elements from the list |
copy() | Returns a copy of the list |
count() | Returns the number of elements with the specified value |
extend() | Add the elements of a list (or any iterable), to the end of the current list |
index() | Returns the index of the first element with the specified value |
insert() | Adds an element at the specified position |
pop() | Removes the element at the specified position |
remove() | Removes the item with the specified value |
reverse() | Reverses the order of the list |
sort() | Sorts the list |
##################################################################################
in next lecture we will discuss about the List
for the blog thanks the * www.W3School.com *
*books that help you to learn python *
* Thank you *
* comment and share and please support *
Comments
Post a Comment