Python Beginner Tutorial # 4 : List

List

is a collection of variables or objects in which there is an order but it is changeable.

colors = ["red","blue","green","black"]
print(colors)
Output :
['red', 'blue', 'green', 'black']

Select items from a list . Keep in mind ranges start from 0 in Python ex. 0,1,2,3….

colors = ["red","blue","green","black"]
print(colors[0])
Output :
red

Printing list one by one

colors = ["red","blue","green","black"]
for x in colors :
    print(x)
Output :
red
blue
green
black

Length of a list

Use the function len() to get the length of a List

colors = ["red","blue","green","black"]
print(len(colors))
Output :
   4

Add items to list

colors = ["red","blue","green","black"]
colors.append("orange")
print(colors)
Output :
['red', 'blue', 'green', 'black', 'orange']

Combine two list together

colors = ["red","blue","green","black"]
colors_2 = ["yellow","pink","white"]
colors.extend(colors_2)
print(colors)
Output :
['red', 'blue', 'green', 'black', 'yellow', 'pink', 'white']

Delete items

Use del to delete an item from a list

colors = ["red","blue","green","black"]
del colors[2]
print(colors)
Output :
["red","blue","black"]
Click here for Part Six : Array