A list is a sequence of values that is similar to a string. However, in a string, the values inside the string or string elements are characters while in the list they can be any type (integers, floats, etc). The values inside the list are called elements or items. All elements of a list are enclosed with square brackets and the list in a general form can be written as:
Example 1 Explore various types of list creation.
Solution: The list can be created in several ways. The most basic one is to create an empty list and save it under the variable name. The empty list is created using Python built-in list() function. In this case, the list will be saved under a variable named list1.
GenList = [item_1, item_2, ...., item_n]As you can see the list consists of n elements (item_1, item_2,...,item_n). The list is enclosed with square brackets [] and the entire list is assigned to the variable name GenList. However, this is not the only type of list definition. In Example 1 we are going to explore several ways of list creation.
Example 1 Explore various types of list creation.
Solution: The list can be created in several ways. The most basic one is to create an empty list and save it under the variable name. The empty list is created using Python built-in list() function. In this case, the list will be saved under a variable named list1.
list1 = list()We can check the type of the variable using the built-in function type.
print("type(list1) = {}".format(type(list1))The previous code line will generate the following output.
type(list1) = < class 'list >So far we have created an empty list. To populate the list with elements we will use for loop.
for i in range(0,11,1):This will populate the list with numbers from 0 to 10. To populate the list with elements we have used the append function. After running the previous code nothing will be displayed as the output. To show the list1 with elements we will type in the following:
list1.append(i)
print("list1 = {}",format(list1))This will generate the following result.
list1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]However, there is a simple way of creating the list in one code line. We can create a list by typing:
list2 = []By writing left and right square brackets we have created an empty list and saved it under variable name list2. To fill the list with elements (integers) from 0 to 10 type the "i for i in range(0,11,1)" inside the brackets.
list2 = [i for i in range(0,11,1)]Now to see the result type the following:
print("list2 = {}".format(list2))which will generate the following output.
list2 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]But how to generate a list with floating point numbers? Let's say we want to create a list with numbers from 0.0 to 1.0 and with a step of 0.01. To do this we have to divide the i element by 100 and set the range from 0 to 101.
list3 = [i/100 for i in range(0,101,1)]This will generate the list with 101 elements in the range from 0.0 to 1.0 with a step of 0.01.
print("list3 = {}".format(list3))
list3 = [0.0, 0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08,So far we have created a list with numbers (integers and floats). Now we will create the list with string elements.
0.09, 0.1, 0.11, 0.12, 0.13, 0.14, 0.15, 0.16,
0.17, 0.18, 0.19, 0.2, 0.21, 0.22, 0.23, 0.24,
0.25, 0.26, 0.27, 0.28, 0.29, 0.3, 0.31, 0.32,
0.33, 0.34, 0.35, 0.36, 0.37, 0.38, 0.39, 0.4,
0.41, 0.42, 0.43, 0.44, 0.45, 0.46, 0.47, 0.48,
0.49, 0.5, 0.51, 0.52, 0.53, 0.54, 0.55, 0.56,
0.57, 0.58, 0.59, 0.6, 0.61, 0.62, 0.63, 0.64,
0.65, 0.66, 0.67, 0.68, 0.69, 0.7, 0.71, 0.72,
0.73, 0.74, 0.75, 0.76, 0.77, 0.78, 0.79, 0.8,
0.81, 0.82, 0.83, 0.84, 0.85, 0.86, 0.87, 0.88,
0.89, 0.9, 0.91, 0.92, 0.93, 0.94, 0.95, 0.96,
0.97, 0.98, 0.99, 1.0]
list4 = ['Apple', 'Orange','Grape']This will generate the following output.
print("list4 = {}".format(list4))
list4 = ['Apple', 'Orange','Grape']