Python Language / Python Lists

It is a collection which is ordered and changeable and is written with square brackets.

Sample Programs
Program Output
# List  Creation List Using list() constructor
ourlist = list(("Welcome to", " Python", "Language"))
print(ourlist)
['Welcome to ', 'Python', 'Language']
# Accessing List Items by using index number.
ourlist = ["Welcome to ",  " Python ",  "Language"]
print(ourlist[ 1])
Python
# Change and print the second item of the list.
ourlist = ["Welcome to ",  " Python ",  "Language"]
ourlist[ 1]= "Python Programming"
print(ourlist[ 1])
Python Programming
# Loop Through a List and print items
ourlist = ["Welcome to ", " Python ", "Language"]
for x in ourlist:
  print(x)
Welcome to Python Language
# Check if Item Exists / Not
ourlist = ["Welcome to ", "Python", "Language"]
if "Python" in ourlist:
  print("Yes Python is there in ourlist")
Yes Python is there in ourlist
#Finds List Length
ourlist = ["Welcome to ", "Python", "Language"]
print( len(ourlist))
3
#Add Items to list using append method()
ourlist = ["Welcome to ", "Python", "Language"]
ourlist.append("Server Side")
print(ourlist)
['Welcome to ', 'Python', 'Language', 'Server Side']
# Add items to  list  at a specified index using insert()
ourlist = ["Welcome to ", "Python", "Language"]
ourlist.insert(2, "Server Side")
print(ourlist)
['Welcome to ', 'Python', 'Server Side', 'Language']
# remove item from list at a specified index
ourlist = ["Welcome to ", "Python", "Language"]
ourlist. remove("Language")
print(ourlist)
['Welcome to ', 'Python']
#pop() removes last item but not specified index
ourlist = ["Welcome to ", "Python", "Language"]
ourlist. pop()
print(ourlist)
['Welcome to ', 'Python']
# del keyword removes the specified index:
ourlist = ["Welcome to ", "Python", "Language"]
del ourlist [0]
print(ourlist)
['Python', 'Language']
#del keyword to a delete a  list completely
ourlist = ["Welcome to ", "Python", "Language"]
del ourlist
print(ourlist)
NameError: name 'ourlist' is not defined
#clear() empties the list
ourlist = ["Welcome to ", "Python", "Language"]
ourlist.clear()
print(ourlist)
[]
#Make a copy of a list with the copy()
ourlist = ["Welcome to ", "Python", "Language"]
copyourlist = ourlist.copy()
print(copyourlist)
['Welcome to ', 'Python', 'Language']
#makes a copy is to use the built-in list().
ourlist = ["Welcome to ", "Python", "Language"]
copyourlist = list(ourlist)
print(copyourlist)
['Welcome to ', 'Python', 'Language']


Home     Back