Python

Creating and indexing lists in Python

We provide effective and economically affordable training courses for R and Python, Click here for more details and course registration !

List is the simplest type of data structure in Python programming. A list is used to store a collection of elements of same type (numeric, string, etc.). In Python, a pair of brackets [] indicates the data object is a list type. For example, the following two statements create two lists, in which one is numeric and the other is of string type.

#create a numeric list
N_list = [31,2,33,44,55]
#

#print the list
N_list
Out[2]: [31, 2, 33, 44, 55]

#create a string list
cars = ['BMW', 'Mercedes', 'Tesla', 'Volvo']

#print the list
print(cars)
['BMW', 'Mercedes', 'Tesla', 'Volvo']

Once a list has been created, its elements can be accessed for use in operation. Accessing elements of a list is called indexing. Python also use brackets [] for indexing. For example, the first element of the list ‘cars’ can be indexed and printed out with specific form.

#The first element of the list is printed out
print(cars[0])
BMW

#the first element of the list is printed with its title form
print(cars[0].title())
Bmw

Note that indexing in Python starts from 0, not 1. And the elements can also be indexed from the end of the list by using a minus – symbol in front of the indexing number. The following code examples show how to perform these operations.

#print the second item of the list
print(cars[1])
Mercedes

#print the fourth item of the list
print(cars[3])
Volvo

#print the last item of the list
print(cars[-1])
Volvo

#print the second item from the end of the list
print(cars[-2])
Tesla

You can easily go one step further to create a new data object by using the value of an existing list. Say, in the following example, the first item of the list ‘cars’ is accessed and concatenated with another string to generate a new string object. Note that Python use plus symbol ‘ + ‘ to concatenate strings.

#the first item of the list 'cars' is accessed and #concatenated with string 'info' and '.', and the result
#is assigned to create a new string object 'info'.
info = "I love " + cars[0].title() + "."

print(info)
I love Bmw.

You can also watch full video on Python tutorial from our YouTube channel.

wilsonzhang746

Recent Posts

Download R Course source files

Click here to download R Course source files !

2 months ago

Download Python Course source files

Click here to download Python Course Source Files !

2 months ago

How to create a data frame from nested dictionary with Pandas in Python

For online Python training registration, click here ! Pandas provides flexible ways of generating data…

5 months ago

How to delete columns of a data frame in Python

For online Python training registration, click here ! Data frame is the tabular data object…

5 months ago

Using isin() to check membership of a data frame in Python

Click her for course registration ! When a data frame in Python is created via…

5 months ago

How to assign values to Pandas data frame in Python

We provide affordable online training course(via ZOOM meeting) for Python and R programming at fundamental…

5 months ago