Python Program: Example of Lists.
#Create the List Items
thislist = ["apple", "banana", "cherry"]
print('1.',thislist)
print('2.',thislist[1])
thislist = ["apple", "banana", "cherry"]
print('1.',thislist)
print('2.',thislist[1])
#Change the List
Items
thislist =
["apple", "banana", "cherry"]
thislist[1] = "blackcurrant"
print('3.',thislist)
thislist[1] = "blackcurrant"
print('3.',thislist)
print('4.',len(thislist))
#Appending
the Existing List
thislist.append("orange")
print('5.',thislist)
#Inserting
an item in 2nd place
thislist.insert(2, "mango")
print('6.',thislist)
#Removing
an item from list
thislist.remove("cherry")
print('7.',thislist)
#Popping
an item from list
thislist.pop()
print('8.',thislist)
#Deleting
an item from list
del
thislist[1]
print('9.',thislist)
if
"apple" in thislist:
print("10. Yes, 'apple' is
in the fruits list")
else:
print("10. No,
'apple' is in the fruits list")
Output
>>>
1. ['apple', 'banana', 'cherry']
2. banana
3. ['apple', 'blackcurrant',
'cherry']
4. 3
5. ['apple', 'blackcurrant',
'cherry', 'orange']
6. ['apple', 'blackcurrant',
'mango', 'cherry', 'orange']
7. ['apple', 'blackcurrant',
'mango', 'orange']
8. ['apple', 'blackcurrant',
'mango']
9. ['apple', 'mango']
10. Yes, 'apple' is in the fruits
list
>>>
'''Remember
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
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
'''
No comments
Post your comments