Python [Post 05] | Lists

A list in Python is an ordered group of items. Items in a list need not be of the same type. A list is created by placing all the items inside a square bracket, separated by commas.

# Lists Examples

my_empty_list = []
number_list = [1,2,3,4,5]
str_list = ['A','B','C','D','Hello Python']
mix_list = ['A',1,'B',2,'C',12.5,5+500j]
nested_list = ['A',1,'B',2,['X','Y','Z']]
print(number_list)
print(str_list)
print(mix_list)
print(nested_list)

[1, 2, 3, 4, 5]
[‘A’, ‘B’, ‘C’, ‘D’, ‘Hello Python’]
[‘A’, 1, ‘B’, 2, ‘C’, 12.5, (5+500j)]
[‘A’, 1, ‘B’, 2, [‘X’, ‘Y’, ‘Z’]]

Some Built-in Python Functions

my_list = [1,2.5,3,4.6,5,6]

print(len(my_list)) # len Function - Gives the total length of the list.
print(max(my_list)) # max Function - Returns element from the list with maximum value.
print(min(my_list)) # min Function - Returns element from the list with minimum value.

6
6
1

### List Indexing & Slicing

Use the index operator [] to access an item in a list. We can use a : to perform slicing which extracts chunk of more than one element as per input. In slicing last index is not included.

sides = ['East','West','North','South']
# Fetch the first element of list sides

sides[0]

‘East’

# Fetch the last element of list sides

sides[-1]

‘South’

# fetch elements start from index 1st till last

sides[1:]

[‘West’, ‘North’, ‘South’]

# fetch elements upto 2nd index, but 2nd is not included

sides [:2]

[‘East’, ‘West’]

# fetch element from 1st index to 3rd index

sides [1:3]

[‘West’, ‘North’]

Unlike Python Strings, Python Lists are mutable i.e. the elements inside a list can be changed!

sides = ['East','West','North','South']
print(sides)

# Let's try to change the elements of lists sides

sides[0]= 3.0
sides[1]='Hello World'
sides[2]='Python World'
sides[3]= 12345

print(sides)

[‘East’, ‘West’, ‘North’, ‘South’]
[3.0, ‘Hello World’, ‘Python World’, 12345]

Nested Lists

A list inside a list.

some_sides = ['East','West','North','South']
more_sides1 = ['North-East','North-West']
more_sides2 = ['South-East','Soth-West']

nested_sides = [some_sides,more_sides1,more_sides2]

print(nested_sides)

[[‘East’, ‘West’, ‘North’, ‘South’], [‘North-East’, ‘North-West’], [‘South-East’, ‘Soth-West’]]

# Fetch the first element of first list

print(nested_sides[0])
print(nested_sides[0][0])

[‘East’, ‘West’, ‘North’, ‘South’]
East

# Fetch the second element of third list

print(nested_sides[2])
print(nested_sides[2][1])

[‘South-East’, ‘Soth-West’]
Soth-West

### Lists Methods

Python includes following list methods

# Creating a new list

sides = ['East','West','North','South']

Append():

Adds single element to the end of a list. Length of the list increases by 1 index.

# Example 1: Append Element in list

sides.append('North-East')
print(sides)

[‘East’, ‘West’, ‘North’, ‘South’, ‘North-East’]

# Other way to append

sides = sides + ['South-East','South-West']
print(sides)

[‘East’, ‘West’, ‘North’, ‘South’, ‘North-East’, ‘South-East’, ‘South-West’]

# Example 2: Append List with List (This will create nested list)

sides = ['East','West']
more_sides = ['North','South','North-East']

sides.append(more_sides)
print(sides)

[‘East’, ‘West’, [‘North’, ‘South’, ‘North-East’]]

Extend():

Add all elements of a list to the another list. It will not create nested list.

# Example - Extending Lists

sides = ['East','West']
more_sides = ['North','South','North-East']

sides.extend(more_sides)
print(sides)

[‘East’, ‘West’, ‘North’, ‘South’, ‘North-East’]

Insert():

Inserts an element at the position mentioned in its arguments. It takes two arguments, position and element.

# Example Insert

sides = ['West','North']

print(sides)

sides.insert(2,'South')
sides.insert(3,'South-East')
sides.insert(0,'East')

print(sides)

[‘West’, ‘North’]
[‘East’, ‘West’, ‘North’, ‘South’, ‘South-East’]

Pop():

Removes and returns an element at the given index. By default pop takes off the last index, but you can also specify which index to pop off.

sides.pop() # Pop off the last indexed item, by default

‘South-East’

print(sides)

[‘East’, ‘West’, ‘North’, ‘South’]

sides.pop(3) # Pop off the 3rd indexed item

‘South’

print(sides)

[‘East’, ‘West’, ‘North’]

Remove():

It is used to delete the first occurrence of item mentioned in its arguments.

sides = ['East','West','North','East','South']

sides.remove('East')
print(sides)

[‘West’, ‘North’, ‘East’, ‘South’]

Clear():

Removes all elements from the list.

sides = ['East','West','North','South']
print(sides)

[‘East’, ‘West’, ‘North’, ‘South’]

sides.clear() # Clear the list

print(len(sides))
print(sides)

0
[]

Index() :

Returns the index of the first matched item.

sides = ['EAST','West','North','East','South','East']

sides.index('East')

3

Count():

Returns the count of number of items passed as an argument.

sides.count('East')

2

Sort():

Sort items in a list in ascending order.

sides = ['West','North','East','South']

print(sides)
sides.sort()
print(sides)

[‘West’, ‘North’, ‘East’, ‘South’]
[‘East’, ‘North’, ‘South’, ‘West’]

Reverse():

Reverse the order of items in the list.

print(sides)
sides.reverse()
print(sides)

[‘East’, ‘North’, ‘South’, ‘West’]
[‘West’, ‘South’, ‘North’, ‘East’]


Thanks!

Happy Learning! Your feedback would be appreciated!

shobhitsingh.in

Github Code

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s