Python

How to use List in Python ?

Python List

A list is a collection which is ordered and changeable. In Python lists are written with square brackets.

Use the code below to create & print a list 

mycolors= [“red”“blue”“green”]
print(mycolors)

You access the list items by referring to the index number, index start from 0, in case you want to access/print the

last item from the list “mycolors” above you can use the code

print( mycolors[2] )

You can even use the Negative indexing means beginning from the end, -1 refers to the last item, -2 refers to the second last item etc. You can print last item in the list using the code print( mycolors[-1] )

You can also specify a range of indexes by specifying where to start and where to end the range.

thislist = [“apple”“banana”“cherry”“orange”“kiwi”“melon”“mango”]
print(thislist[2:5])

The code above will Return the third, fourth, and fifth item

Note: The search will start at index 2 (included) and end at index 5 (not included).

We can use the code below to change the value of any item in the list :

mycolors= [“red”“blue”“green”]
mycolors[1] = “black”

Check list length

mycolors= [“red”“blue”“green”]
print(len(mycolors))

Check if Item Exists

To determine if a specified item is present in a list use the in keyword:

mycolors= [“red”“blue”“green”]
if “blue” in mycolors:
  print(“Yes, ‘blue’ is in the mycolors list”)

Add Items

To add an item to the end of the list, use the append() method:

mycolors= [“red”“blue”“green”]
mycolors.append(“orange”)
print(mycolors)

To add an item at the specified index, use the insert() method

Insert an item as the second position:

mycolors= [“red”“blue”“green”]
mycolors.insert(1,“orange”)
print(mycolors)

Remove Item

There are several methods to remove items from a list

The remove() method removes the specified item:

mycolors= [“red”“blue”“green”]
mycolors.remove(“blue”)
print(mycolors)

The pop() method removes the specified index, (or the last item if index is not specified):

mycolors= [“red”“blue”“green”]
mycolors.pop()
print(mycolors)

The del keyword removes the specified index:

mycolors= [“red”“blue”“green”]
del mycolors[0]
print(mycolors)

The del keyword can also delete the list completely:

mycolors= [“red”“blue”“green”]
del mycolors

The clear() method empties the list:

mycolors= [“red”“blue”“green”]
mycolors.clear()
print(mycolors)

Make a copy of a list with the copy() method:

mycolors= [“red”“blue”“green”]
mylist = mycolors.copy()
print(mylist)

Make a copy of a list with the list() method:

mycolors= [“red”“blue”“green”]
mylist = list(mycolors)
print(mylist)

How to Print all items in the list using loop :

mycolors= [“red”“blue”“green”]
for x in mycolors:
  print(x)

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

You can find more Python Tutorials  on https://www.w3schools.com/python

Comment here

9 + 1 =