Loading...

Python tutorial

Lists in Python

  • List allow you to store multiple items in one place.
  • List is a collection which is ordered and changable.
  • Lists are written with square brackets.
Fruits_list = ["apple", "banana", "mango"]
print(Fruits_list)

 output:

['apple', 'banana', 'mango']

You can access list items through their position in the list or Index.

The first item is always index zero.

A negative index access the list from the other end.

Fruits_list = ["apple", "banana", "mango"]

print(Fruits_list)

print(Fruits_list[0])

print(Fruits_list[-1])

output:

['apple', 'banana', 'mango']

apple

mango

append()

use append() to add single items to the end of a list.

Fruits_list = ["apple", "banana", "mango"]

Fruits_list.append('cherry')

print(Fruits_list)

Output:

['apple', 'banana', 'mango', 'cherry']

insert()

use insert() to insert items at any position useing the index of that position.

Fruits_list = ["apple", "banana", "mango", 'cherry']

Fruits_list.insert(0,'grapes')

print(Fruits_list)

Output:

['grapes','apple', 'banana', 'mango', 'cherry']

Modify/Replace

modify an element in a list useing its index

Fruits_list = ["apple", "banana", "mango", 'cherry']

Fruits_list[-1]='CHERRY'

print(Fruits_list)

Output:

['apple', 'banana', 'mango', 'CHERRY']

This is how we replace or modify items in a list


Removing Items from a List

Removing items from a list by useing

  • remove()
  • del
  • pop()

remove()

The remove() method removes a specific item from a list

Fruits_list=['grapes','apple', 'banana', 'mango', 'cherry']

Fruits_list.remove('grapes')

print(Fruits_list)

Output:

['apple', 'banana', 'mango', 'cherry']

if an item appears more than once in the list, only its first appearance is removed.

Fruits_list=['grapes','apple', 'banana', 'mango', 'cherry','grapes']

Fruits_list.remove('grapes')

print(Fruits_list)

Output:

['apple', 'banana', 'mango', 'cherry', 'grapes']

del

the del keyword delets an item at specific position in a list useing its index.

Fruits_list=['grapes','apple', 'banana', 'mango', 'cherry']

del Fruits_list[0]

print(Fruits_list)

Output:

['apple', 'banana', 'mango', 'cherry']

pop()

The pop( ) method removes the last item in a list by default.

The pop() method removes the element at the specified position.

Fruits_list=['grapes','apple', 'banana', 'mango', 'cherry']

Fruits_list.pop()

print(Fruits_list)

Fruits_list.pop(2)

print(Fruits_list)

Output:

['grapes', 'apple', 'banana', 'mango']
['grapes', 'apple', 'mango']

Slicing a List

Slice is a part of a list that begins with the item at the first index specified and stops with the item before the last index is specified.

1)Slice Range of items

Fruits_list=['grapes','apple', 'banana', 'mango', 'cherry']

print(Fruits_list[1:3])

Output:

['apple', 'banana']

2)Slice First two items

Fruits_list=['grapes','apple', 'banana', 'mango', 'cherry']

print(Fruits_list[:2])

Output:

['grapes', 'apple']

3)Slice Last two items

Fruits_list=['grapes','apple', 'banana', 'mango', 'cherry']

print(Fruits_list[-2:])

Output:

['mango', 'cherry']

copy a list

A copy of a list lets you work with the cintents of the copied list without affecting the orginal list.

fruits = ["apple", "banana", "cherry"]

x = fruits[:]

print(x)

Output:

['apple', 'banana', 'cherry']

Changes you make to the new list dont affect the original list

fruits = ["apple", "banana", "cherry"]

x = fruits[:]

print(x)

x.append('dragon fruit')

print(x)
print(fruits)

Output:

['apple', 'banana', 'cherry']
['apple', 'banana', 'cherry', 'dragon fruit']
['apple', 'banana', 'cherry']

Looping a list

use a for loop to loop through all items in a list

fruits = ["apple", "banana", "cherry"]

for x in fruits:
    print(x)

Output:

apple
banana
cherry

We can also use slice in a loop

fruits = ["apple", "banana", "cherry"]

for x in fruits[:2]:
    print(x)

Output:

apple
banana

Sorting Lists

The sorted() function returns a copy of a list in a natural sorted order.the order of the orginal list is not affected

fruits=['apple','mango','cherry','strawberry','dragon fruit']

print(sorted(fruits))
print(fruits)

Output:

['apple', 'cherry', 'dragon fruit', 'mango', 'strawberry']

['apple', 'mango', 'cherry', 'strawberry', 'dragon fruit']

To sort a list,use the sort() method

fruits=['apple','mango','cherry','strawberry','dragon fruit']

fruits.sort()
print(fruits)

Output:

['apple', 'cherry', 'dragon fruit', 'mango', 'strawberry']

Reverse Sorting Lists

to sort a list in reverse order,use the reverse argument set to True

fruits=['apple','mango','cherry','strawberry','dragon fruit']
x=sorted(fruits,reverse=True)
print(x)

Output:

['strawberry', 'mango', 'dragon fruit', 'cherry', 'apple']

The reverse argument works with sort() and sorted().To reverse the orginal order of a list,use the reverse() method

fruits=['apple','mango','cherry','strawberry','dragon fruit']
fruits.reverse()
print(fruits)

Output:

['dragon fruit', 'strawberry', 'cherry', 'mango', 'apple']

the reverse() method reverses the orginal order of a list


List Comprehensions

A list comprehensions is a compact way of defining a list in one line

for exp in range(5):
    print(10**exp)

Output:

1
10
100
1000
10000

Lets see how to write above logic in List Comprehensions

num=[10**exp for exp in range(5)]
print(num)

Output:

[1, 10, 100, 1000, 10000]

Numerical Lists

The range() function generates a series of numbers.Giving a range() one argument returns a series of numbers from 0 upto,not includeing the  number

num=list(range(5))
print(num)

Output:

[0, 1, 2, 3, 4]

Pass two values to create a range that starts with the first value and ends at one less than the second value.

num=list(range(5,10))
print(num)

Output:

[5, 6, 7, 8, 9]

Use a third argument to specify a distance to skip between numbers in a list

num=list(range(5,10,2))
print(num)

Output:

[5, 7, 9]