Hey Everyone,I am Vikram Salunke and welcome to Python for InfoSec.
This is part 8 and in this post we will see Lists

List data structure in Python provides an excellent way for storing arrays of objects in Python

They allows to store sets of information in one place for few items or hundreds of items

It is collection of items such as phone numbers, names of all the people fruits you like etc.

Let's create a list of fruits you like


for list we use a square brackets and two objects are seperated by commas

fruits = ['mango', 'banana', 'apple', 'watermelon']

print fruits

We can also access particular elements from a list by using location



Just like string the list also starts with the 0 so mango is at 0th position banana is at 1st position and so on

To calculate the length of a list we use len(fruits)

list len

It will give the length of a list

We can also modify particular element from a list

let's change 3rd to the "pineapple"

fruits[3] = "pineapple"

modify list

ok, it changed the watermelon to pineapple

let's create another list of numbers

numbers = [102,22,232,452]


We can also append a particular element to the list using an append method


list append

ok, so 564 is appended using an append method

we can also sort the list using numbers.sort()

list sort

all numbers are sorted

We can also insert element at particular position

for example, let's enter 677 at 2nd position


list insert

As we can see at second position we have entered number 677

And once again list start from 0 so 22 is at 0th position, 102 is at 1st position, 677 is at 2nd position and so on

We can also delete by using del numbers[1]

And we can also remove by giving value such as numbers.remove(677)

list delete

We can construct list of any data type and as we saw earlier there are built-in methods for appending, inserting removing, counting etc.

Let's create a new list


as we can see there are elements from different data types

That's it for Lists

If you want to join our in person class then please click here we are doing trainings in some upcoming InfoSec conferences.
And if you have any doubts, any questions then you can tweet me @vikramsalunke20
We are uploading daily new videos on YouTube click here to subscribe.
If you want to solve the exercises on this topic those are below. Solve the exercises and check your understandings. Thank you :)