Python Tutorial | Python List

How to create a list?

Python offers a range of compound data types often referred to as sequences. List is one of the most frequently used and very versatile data type used in Python.

In Python programming, a list is created by placing all the items (elements) inside a square bracket [ ], separated by commas.
It can have any number of items and they may be of different types (integer, float, string etc.).

# to create an empty list
n = []

# list with list of integers
n = [1, 2, 3]

# list with mixed datatypes
n = [1, “Hello”, 3.4]

Also, a list can have another list as an item. This is called nested list.
# nested list
n = [“hello”, [8, 4, 6], [‘a’]]

How to access elements from a list?

There are various ways in which we can access the elements of a list.

List Index

We can use the index operator [] to access an item in a list. Index starts from 0. So, a list having 5 elements will have index from 0 to 4.

Trying to access an element other then the range will raise an IndexError. The index must be an integer. We can’t use float or other types, this will result into TypeError.

Nested list are accessed using nested indexing.

Examples:

n1=[‘c’ , ‘o’ , ‘m’ , ‘p’ , ‘u’ , ‘t’ , ‘e’ , ‘r’]

0

1

2

3

4

5

6

7

c

O

m

p

u

t

e

r

print(n1)
#output
[‘c’, ‘o’, ‘m’, ‘p’, ‘u’, ‘t’, ‘e’, ‘r’]

print(n1[0])
#output
c

print(n1[2])
#output
m

print(n1[4])
#output:
u

print(n1[2.5]) #error
#output:
error

example:

Output:

[‘c’, ‘o’, ‘m’, ‘p’, ‘u’, ‘t’, ‘e’, ‘r’]
c
m
u
Traceback (most recent call last):
File “C:/Python37/ttt1.py”, line 6, in <module>
print(n1[2.5])
TypeError: list indices must be integers or slices, not float
>>>

example:

n2=['c','a','t','a','l','y','s','t']
print(n2)
print(n2[0])
print(n2[2])
print(n2[5])
print(n2[7])
print(n2[1])
print(n2[1.7])

Output:

[‘c’, ‘a’, ‘t’, ‘a’, ‘l’, ‘y’, ‘s’, ‘t’]
c
t
y
t
a
Traceback (most recent call last):
File “C:/Python37/ttt1.py”, line 8, in <module>
print(n2[1.7])
TypeError: list indices must be integers or slices, not float
>>>

# Nested List and Nested indexing

n=[“Happy”, [2,0,1,5]]

print(n[0][0])
#Output:
H
print(n[0][1])
#Output:
a
print(n[1][0])
#Output:
2
print(n[1][1])
#Output:
0
print(n[1][3])
#Output:
5

Example:

n=["Happy", [2,0,1,5]]
print(n[0][0])    
print(n[0][1])    
print(n[1][0])
print(n[1][1])
print(n[1][3])

Output:

H
a
2
0
5
>>>

Example:

n=["Catalyst", [23,10,31,52,123]]

print(n[0][0])    
print(n[0][1])
print(n[0][2])
print(n[0][3])
print(n[0][4])

print(n[1][0])
print(n[1][1])
print(n[1][3])
print(n[1][4])
print(n[1][2])

Output:

C
a
t
a
l
23
10
52
123
31
>>>

Python Basic Programming Tutorial

Python Introduction     Getting started in Python Programming      Python propgramming fundamentals     Python Operators    Python If Condition     Python for loop    Python range construct      Python While loop    break and continue statements     Different looping techniques     Python List     Python String     Python Functions    Python Inbuilt Functions     Python Recursion     Using Python Library     Python Tuples     Python Dictionary     Python Sets     Python Strings     Python Exception Handling     Python Data File Handling