List

Lists are a fundamental data structure in Python used to store multiple items in a single variable. These multiple items can be of same or different data type.

Lists are:

  1. Changeable - we can change, add, or remove the items of a list.
  2. Ordered - items in a list have a definite order. If we add items to list, they will be added to the end.
  3. Redundant Data - items of a list can be repeated.
  4. Indexing - list items have indexes. First element has 0 index, second has 1 and so on.

Members of a List

Members of a list are changeable and can be of any data type. We can also calculate the number of items in our list.

Data Types of List Members

Members of a list can be of any data type. They can have same or different data types.

Repetition

Members of a list can be repeated multiple times.

Indexes in a List

Items in a list start from 0 index.

Range of Index

You can also specify the range of index.

Negative Index

Negative indexes start from the end of the list and the last element has -1 index. You can access the last element of list by giving -1 index.

Changing Elements

We can also change elements in a list.

Changing Elements in Range

We can change elements in range.

Adding Elements

We can add elements in list at specified index.

List Comprehension

List comprehension is a powerful and concise way to create lists in Python. It provides you a shorter syntax to create a new list based on the existing list.

Join two lists

There are many ways to join two lists in Python. We can use "+" operator to join two lists.

We can also use append or extend methods to join two lists. These methods are given in methods section.

List Methods

We can apply different methods on list.

OperatorCodeDescription
.insert(index, item)fruits.insert(1, "Mango")Insert items at specified index.
.append(item)cars.append("Gazoo")Add items at the end of the list.
.extend(iterable)femaleStds.extend(maleStds)Append another list in current list.
.remove(item)nums.remove(5)Remove specific item.
.pop(index)poppedItem = nums.pop(2)Remove item at specific index.
.del()del nums[2]Remove specific index. Also delete the list as a whole. List does not exists.
.clear()nums.clear()Empties the list. List exists with no element.
.sort()nums.sort()Sort list in ascending order.
.sort(reverse = True)nums.sort(reverse=True)Sort list in descending order.
.sort(key = str.lower)fruits.sort(key=str.lower)Perform case insensitive sorting.
.sort(key = str.lower, reverse)fruits.sort(key=str.lower, reverse = True)Sort list in descending order regardless of case of alphabets.
.reverse()nums.reverse()Reverse the order of the list.
.index(item, start, end)index1 = nums.index(20, 0, 4)Returns the index of the element.
.count(item)names.count("Daniel")Count number of items in the list.
.copy()favCars = cars.copy()Returns the copy of the list.
list()fav_fruits = list(fruits)Returns the copy of the list.
len()len(fruits)Returns the number of items in the list.
slice(start, stop, step)sub_list = nums[2:8:2]Returns a slice of the list from start index to stop index with a given step.
min()min(nums)Returns the smallest item in the list (only works if the items are comparable).
max()max(nums)Returns the largest item in the list (only works if the items are comparable).
sum()sum(nums)Returns the sum item in the list (only works for numeric type data).
all()all(items)Returns true if all the elements of the list are true or if the list is empty.
any()any(items)Returns true if any one of the elements of the list are true or if the list is not empty.
enumerate()for index, value in enumerate(cars): ...Returns an iterator that generates pairs of index and value for each item in the list.
reversed()reversed(nums)Returns a reverse iterator over the list's elements.

Practice Exercises

Complete these exercises to reinforce your learning and earn XP

Sign in to track your progress and earn XP!
Exercise 1 of 2Easy

Which of the following is a best practice in programming?

10 XP~2 min
Exercise 2 of 2Easy

Code that is easy to read and understand is called ___ code.

10 XP~2 min