Understanding lists in Python

In this tutorial we will be focussing on Lists in Python, We will understand lists in Python from scratch to advanced.

List is a group which is disordered and variable. It allows matching members. It is created by placing all the members seperated by commas in a square bracket[].

>>> values = [2,4,7,9]

# We have list of values, names and groups. Normally its not permitted to use unalike values at distinct locations in lists but python authorize it. So, we have liberty of using dissimilar parts of lists having seperate types. Location of characters in lists is same as it was in string i.e, from 0 to n-1 where n is length of list. Length of list is given by len().

>>> values = [2,4,7,9]
>>> names = ["tina", "kajal", "priti"]
>>> group = [True, "tiger", 4]
>>> values[0]
2              //output//
>>> values[2:3]
[7]          //output//

Here, if we asked for 0th position, then it will give us the value 2, in second output you can see 2 stands for 3rd position and 3 stands for 4th, so number 7 is at 3rd place and as per rule 4th-1 is 3rd position so output will be number 7. .

Types of Values

There are 2 types of values- 1. ‘int’ which represents whole numbers and integers. 2. ‘float’ which represents values having decimal point.

For comparison between values, we use equal to ‘==’, not equal to ‘!=’, greater than ‘>’, less than ‘<‘, etc. Result ofsuch comparison is Bool value. To make compund conditions we can combine this comparisons using ‘not’ and ‘and’ operators.

Logical values are used like bool, True and False which carry out and, or and not operations.

Arithmetic operations like +, -, *, / —- are used to construct math library.

Nested List

Nested lists are lists consisting of other lists. This is an amazing feature in Python. It is a bright and succint way of creating of utilizing lists.

>>> nested = [[6, [90]], 21, ["sita"]]

[6, [90]] is the 1st position, 21 is the second position and [“sita”] is the second position. If we want to perceive the position of ‘t’-

>>> nested = [[6, [90]], 21, ["sita"]]
>>> nested[0]
[6, [90]]            //output//

# Some more examples are-

>>> nested[1]
21              //output//
>>> nested[2]
['sita']          //output//
>>> nested[2][0][2]
't'         //output//

# 2 is the position of the 3rd list, 0 is a string and 2 is the position of character ‘t’.

Slicing concept

When we take a slice of list, we get a list.

>>> nested = [[6, [90]], 21, ["sita"]]
>>> nested[0][1:3]
[[90]]           //output//

In lists, we can renovate it in place. Here, we can change the places of characters.

>>> nested = [[6, [90]], 21, ["sita"]]
>>> nested[1] = 5
>>> nested
[[6, [90]], 5, ['sita']]           //output//

# We can also change the positions of characters inside the list.

>>> nested[0][1][0] = 19
>>> nested
[[6, [19]], 5, ['sita']]             //output//

So, this types of changes are allowed, thats why Lists are mutable.

Difference between Mutable and Immutable values.

Suppose,

>>> a = 1
>>> b = a
>>> a = 2

has the value of b changed?

>>> a = 1
>>> b = a
>>> b
1              //output; as expected//

>>> a = 2
>>> b
1         //output; still the same//

So, the values of b did not changed because ‘b’ duplicates the value of ‘a’ and makes a new duplication i.e, if we change value of ‘a’ later, it does not affect value of ‘b’. For immutable values, assignment makes new edition of value.

>>> list1 = [4, 23, 45, 67]
>>> list2 = list1
>>> list1[2] = 34
>>> list1
[4, 23, 34, 67]                  //output//
>>> list2
[4, 23, 34, 67]           //output//
>>> list2[2]
4             //output//

This is a contradiction of the statement we made before. List1 and List2 are names of same list. List1 is having some data and List2 is just another name of List1. So, when you changes the value of List1, List2 also has same value at that position. Thus, for mutable values, allocation does not make new version but it points both names to same values.

Transcribing Lists

Slice takes a list and returns us a sublist from one position to another position. End result of slice operation gives a new list. If we delete both the beginning and ending character it gives us a full slice. [ : ] == [0 : len(l)] To imitate a list we must use full slice.

>>> list1 = [3, 5, 2, 7, 8]
>>> list2 = list1[:]
>>> list1[3] = 9
>>> list1
[3, 5, 2, 9, 8]          //output//

# We have first list, list2 is the slice.

>>> list2
[3, 5, 2, 7, 8]     //output//

# but list2 will not affected as it was just a copy of list1. It has nothing to do with the data which is added later. By taking Slice, we get a new list, take that entire list as a Full slice, we’ll get a full copy of old list vand assign some new name to it.

Deviation on Equality

We will generate 2 lists having same data and we’ll produce a new list3 and we’ll allot the same value as list2. list3 is also pointing same thing. All 3 lists looks identical, but there is actually a contrast. List1 and List2 are 2 lists with same value. List2 and List3 are 2 names for same list. Python has ‘==’ operation which examines whether 2 lists have same value. This will show that the list1 and list2 are equal. Python has ‘is’ keyword to check the equality of list2 and list3.

>>> list1 = [3, 5, 2, 7, 8]
>>> list2 = [3, 5, 2, 7, 8]
>>> list3 = list2
>>> list1 == list2
True     //output//
>>> list2 == list3
True     //output//
>>> list2 is list3
True       //output//
>>> list1 is list2
False        //output//

# list1 is list2 is False because they both are different lists.

>>> list2[3] = 9
>>> list2
[3, 5, 2, 9, 8]        //output//
>>> list1 == list2
False          //output//
>>> list2 == list3
True         //output//
>>> list3
[3, 5, 2, 9, 8]    //output//

# in above lines, list1 is a different list so itn won’t change its data. Both are same lists, by changing its data it will also get affected.

Concatenation

We can also use the concatenation function in Lists by merging many lists using ‘+’ operator.

>>> list1 = [3, 5, 2, 9, 8]
>>> list2 = [2, 3, 0, 4, 6]
>>> list3 = list1 + list2
>>> list3                                                                                                                         [3, 5, 2, 9, 8, 2, 3, 0, 4, 6]                //output//

‘+’ operator invariably produces new list.

>>> list1 = [3, 5, 2, 9, 8]                                                                                              >>> list2 = list1                                                                                                            >>> list1 = list1 + [12]                                                                                                >>> list1                                                                                                                         [3, 5, 2, 9, 8, 12]               //output//

# So, now list1 will change but list2 will remain as it is.

Leave a comment