Python List example

In this post, we are going to discuss Python Lists.
Python Lists are one of the most used data structure in the python.

Python has 6 types of Sequence and Lists are the most common version of sequence in Python. Python Lists are mutable and can be changed unlike Strings.

You need to understand two things about List which you might find different from other programming language.

  • List has no fixed length
  • List has no fixed type

In this section we will learn about:

Lists are created with brackets [] and commas separating every element in the list.


Constructing a list

We just created a list of integers, but lists can actually hold different object types unlike java.
For example:

Create empty list and add items to it

list3 =[] list3.append('one') list3.append('two') list3.append('three') print(list3)

Output:

[‘one’, ‘two’, ‘three’]

Indexing and Slicing

It is very much similar to String.Let’s check with the help of examples

my_list = [1,'A','B',"CD",2] #print List print(my_list) # get element at index 1 print(my_list[0]) # get index 1 and everything after it print(my_list[1:]) # get everything UP TO index 3 print(my_list[:3])

Output:

[1, ‘A’, ‘B’, ‘CD’, 2]
1
[‘A’, ‘B’, ‘CD’, 2]
[1, ‘A’, ‘B’]

We can also use + to concatenate lists,same as the String

my_list=[1,2,3,4] print(my_list + ['Added item']) #Above statement won't change the list print(my_list) # You would have to reassign the list to make the change permanent. #Reassign my_list = my_list + ['new item'] print(my_list)

Output:

[1, 2, 3, 4, ‘Added item’]
[1, 2, 3, 4]
[1, 2, 3, 4, ‘new item’]

We can also use the * for a duplication method similar to strings:

You can use * symbol to multiply the number of items in the list.

my_list=[1,2,3,4] print(my_list * 2) print(my_list) #Reassign my_list=my_list * 2 print(my_list)

Output:

[1, 2, 3, 4, 1, 2, 3, 4]
[1, 2, 3, 4]
[1, 2, 3, 4, 1, 2, 3, 4]

Basic List Methods

Let’s go ahead and explore some more special methods for lists:

len

Just like strings, you can use len() function to find number of items in the list

output:

4

append

Use the append method to permanently add an item to the end of a list

output:

[‘a’, ‘b’, 3, ‘append’]

pop

pop method can be used to remove element from the list.If you do not specify any parameter,pop method will remove element from last index but you can pass index too to remove corresponding element.
Let’s see an example:

output:

‘a’
[‘b’, 3, ‘append’]

It should also be noted that lists indexing will return an error if there is no element at that index. For example:

—————————————————————————
IndexError Traceback (most recent call last)
in ()
—-> 1 l[100]

IndexError: list index out of range

sort

We can use the sort method and the reverse methods to also effect your lists:

[55,34,13,22,10]
[10,13,22,34,55]

Nesting Lists

Nested list means list in another list.There are times when you need to create nested datastructures.

Let’s see how this works!

# Let's make three lists lst_1=[1,2,3] lst_2=[4,5,6] lst_3=[7,8,9] # Make a list of lists to form a matrix matrix = [lst_1,lst_2,lst_3]

Output:

[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

Now we can again use indexing to get elements, but now there are two levels of the index. The items in the matrix object, and then the items inside that list!

[1, 2, 3]

# get first item of the first item in the matrix object

1

Leave a Reply

Your email address will not be published. Required fields are marked *