Python Lists

There are 4 built in data types to store collections of data, they are List, Tuple, Set, and Dictionary.

Python Lists
Python Lists

There are 4 built in data types to store collections of data, they are List, Tuple, Set, and Dictionary.

To store multiple items in a single variable we use array like data type in other programming languages, instead of array in python we use list data type.

Python list is ordered, flexible, mutable, and indexed.

  • Ordered: Python list have a defined order, that’s which items comes before others.
  • Flexible: Python list is flexible to store any types of elements such as integer, string, float, or mixture of all.
  • Mutable: Python list is changeable that’s we can add new items, update items, or remove items.
  • Indexed: Python list is indexed. The first index is zero, second is one, and so on. Python list allows duplicate elements.

Creating a List

To create a list we need to use square brackets[] to enclose the elements and the values are separated with comma(,).

Example:

mylist = ["Java", "Python", "HTML", "CSS"]
print(mylist)

Output:

['Java', 'Python', 'HTML', 'CSS']

Alternatively, we can use list() constructor method to create list.

Example program to create list using list() method:

mylist = list(("Java", "Python", "HTML", "CSS"))
print(mylist)

Output:

['Java', 'Python', 'HTML', 'CSS']

Access elements in List

To access values in list use square brackets along with element’s index value(every element in the list is indexed, so we can access elements in list by referring its index number).

Note: The first item has the index value 0, second item has 1, and it will goes on.

Example:

mylist =["Java", "Python", "HTML", "CSS"]
print(mylist[2])

Output:

HTML

List Slicing

For slicing list, we want to specify range of index from where to start and where to end the range.

Example:

mylist =[1, 2, 3, 4, 5, 6, 7, 8, 9]
print(mylist[1:5])

Output:

[2, 3, 4, 5]

Negative indexing

In python list we can access the elements in the list by refering it’s negative index. Negative index starts from the end.

The value for last item is -1.

Example:

mylist =[1, 2, 3, 4, 5, 6, 7, 8, 9]
print(mylist[-3])

Output:

7

Add new item to a list

append() method

We can add elements to the list by using append() function. We can add only one item in list by using append() function. This will add item at the end of the list.

Example:

mylist =["Python", "HTML", "Java", "CSS"]
mylist.append("JavaScript")
print(mylist)

Output:

['Python', 'HTML', 'Java', 'CSS', 'JavaScript']

extend() method

extend() method is used to add items from another list to the current list.

Example:

mylist = ["Python", "HTML", "Java", "CSS"]
mylist1 = ["JavaScript", "R", "C++", "C#"]
mylist. extend(mylist1)
print(mylist)

Output:

['Python', 'HTML', 'Java', 'CSS', 'JavaScript', 'R', 'C++', 'C#']

insert() method

insert() method is used to add items at the desired position(index) and it requires two arguments that’s position(index) and value.

mylist = ["Python", "HTML", "Java", "CSS"]
mylist. insert(2, "C++")
print(mylist)

Output:

['Python', 'HTML', 'C++', 'Java', 'CSS']

Removing Items from the list

remove() method

remove() method is used to remove only one element at a time.

Example:

mylist = ["Python", "HTML", "Java", "CSS"]
mylist. remove("HTML")
print(mylist)

Output:

['Python', 'Java', 'CSS']

pop() method

pop() method is used to remove particular element by referring its index value. If the index value is not specified it will remove the last element.

Example:

mylist = ["Python", "HTML", "Java", "CSS"]
mylist. pop(2)
print(mylist)

Output:

['Python', 'HTML', 'CSS']

clear() method

clear() method is used to empty the list and it will return an empty list as output.

Example:

mylist = ["Python", "HTML", "Java", "CSS"]
mylist. clear()
print(mylist)

Output:

[]

del keyword

We can use del keyword to delete particular element in a list, we can delete multiple elements in a list by specifying the range of index and we can delete entire list by specifying the name of the list.

Example to delete particular element

We can delete a particular element in a list by referring its index number.

mylist = ["Python", "HTML", "Java", "CSS"]
del mylist[1]
print(mylist)

Output:

['Python', 'Java', 'CSS']

Example to delete multiple elements

To delete multiple elements in a list we want to specify the starting and ending index value.

mylist = ["Python", "HTML", "Java", "CSS", "JavaScript", "C", "C++"]
del mylist[1:4]
print(mylist)

Output:

['Python', 'JavaScript', 'C', 'C++']

Example to delete entire list

To delete entire list we want to specify the list name.

Note: When we delete the specific list and try to print the list it will show error.

mylist = ["Python", "HTML", "Java", "CSS", "JavaScript", "C", "C++"]
del mylist
print(mylist)

Output:

Traceback (most recent call last):
  File "mylist.py", line 3, in 
    print(mylist)
NameError: name 'mylist' is not defined

len() method

len() method is used to find the total length of the list.

Example:

mylist = ["Python", "HTML", "Java", "CSS", "JavaScript", "C", "C++"]
print(len(mylist))

Output:

7

count() method

count() will return how many times the particular element occurs in list.

Example:

mylist = ["Python", "HTML", "Java", "CSS", "JavaScript", "C", "C++", "Java", "C#", "Java"]
print(mylist.count("Java"))

Output:

3

sort() method

sort() method is used to sort the items in a list in ascending order.

Example:

mylist = ["Python", "HTML", "Java", "CSS", "JavaScript", "C", "C++", "C#", "PHP"]
mylist.sort()
print(mylist)

Output:

['C', 'C#', 'C++', 'CSS', 'HTML', 'Java', 'JavaScript', 'PHP', 'Python']

reverse() method

reverse() method is used to reverse the order of elements in the list.

Example:

mylist = ["Python", "HTML", "Java", "CSS", "JavaScript", "C", "C++", "C#", "PHP"]
mylist.reverse()
print(mylist)

Output:

['PHP', 'C#', 'C++', 'C', 'JavaScript', 'CSS', 'Java', 'HTML', 'Python']

copy() method

copy() function will return a copy of the original list, any changes in the original list will not affect in the copied list.

Example:

mylist = ["Python", "HTML", "Java", "CSS", "JavaScript" ]
mylist1 = mylist.copy()
print(mylist1)

Output:

['Python', 'HTML', 'Java', 'CSS', 'JavaScript']

max() and min() method

max() method will return the maximum number from the list and the min() method will return the minimum number from the list.

Example:

mylist = [2, 5, 3, 75, 23, 54, 22, 85, 28, 12, 3, 24, 87]
print(max(mylist))
print(min(mylist))

Output:

87
2