0% found this document useful (0 votes)
7 views14 pages

Class 13 List

Uploaded by

shivangiupa123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views14 pages

Class 13 List

Uploaded by

shivangiupa123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

PYTHON LIST

Python by Computer G
LIST A list is a collection which is
ordered and changeable. In Python
lists are written with square
brackets.
Example Create a List:
thislist = ["apple", "banana", "cherry"]
print(thislist) # returns ['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]) # returns banana
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] = "grapes" print(thislist)
# returns ['apple', 'grapes', '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")
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)) # returns 3
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) # returns ['apple', 'cherry']
POP() METHOD
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)
# returns ['apple', 'banana']
DEL
Example The del keyword removes the specified index:
thislist = ["apple", "banana", "cherry"]
del thislist[0]
print(thislist) # returns ['banana', 'cherry']
Example The del keyword can also delete the list
completely:
thislist = ["apple", "banana", "cherry"]
del thislist
CLEAR()
Example The clear() method empties the list:
thislist = ["apple", "banana", "cherry"]
thislist.clear()
print(thislist) # returns [ ]
COPY A LIST
You cannot copy a list simply by typing list2 = 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()
COPY()
Example Make a copy of a list with the copy() method:
thislist = ["apple", "banana", "cherry"]
mylist = thislist.copy()
print(mylist)
# returns ['apple', 'banana', 'cherry']
COPY A LIST
Example Make a copy of a list with the list() method:
thislist = ["apple", "banana", "cherry"]
mylist = list(thislist)
print(mylist) # returns ['apple', 'banana', 'cherry']
Thanks For Watching

You might also like