Search⌘ K

List of Values

Explore how to create and use lists in Python, accessing values through positive and negative indices. Learn slicing techniques to extract subsets and practice list operations with example programs to build foundational skills in Python list manipulation.

What is a list?

In Python, a list is a comma-separated collection of values enclosed in square brackets and stored as a single variable. For example, ["apple","mango","banana"] is a list of strings. It can be stored as a variable just like any other data type.

The following program demonstrates how to create a list of string values:

Python 3.10.4
listOfFruits = ["apple","mango","banana"] # Creating a list of strings
print(listOfFruits)

In the code above:

  • We use a variable, listOfFruits, to assign the comma-separated values enclosed in square brackets.
  • We call the variable, listOfFruits, a list.

The following program demonstrates the various types of lists in Python:

Python 3.10.4
list0 = []
print("Empty list: ")
print(list0)
list1 = [100]
print("\nSingle-valued list: ")
print(list1)
list2 = [7, 7, 4, 4, 3, 3, 3, 6, 5]
print("\nList having duplicate Numbers: ")
print(list2)
list3 = [50, 'Learn', 10, 'To', 2.5, -1, 'Code']
print("\nList with the use of mixed values: ")
print(list3)

Hint: The \n written as part of the string in the print statements is used to display an extra line.

There are a few new things we have to understand in the code above.

  • list0, list1, list2, list3, list4, and list5 are the variables storing list values.

  • A list that has no values is an empty or a blank list (list0).

  • A list can have only one value (list1).

  • The variable list2 holds a list of integers. The ...