Lists in Python

Before going into List, an in-built data structure in python , Let us understand what's a data structure ? Data structures allows us to store and organize data efficiently. After storing data we can easily access and perform operations on this data.

Python has four in-built data structures:

  1. Lists
  2. Tuples
  3. Sets
  4. Dictionaries

Now let us look into List data structure in deep.

Lists

  • Lists are versatile python data structures and holds data in a ordered sequence. We call it versatile because it is easily changeable.
  • In other words we call it Mutable because we can change the elements in place after creating a list.
  • The elements of list are enclosed in Square Brackets ([ ]) and each item is separated by a comma(,).
list_a=[1,3,4,'True','Hello',9]

The elements of a list can be accessed by indexing. The index of first element being '0'.To access elements from rarer side we use negative indexing , The index of first element from rarer side being '-1'.

list_a = [1,3,4,'True','Hello',9]
//To access 1 in list_a
print(list_a[0]) //Output : 1
//To access last element of list, Here 9 is last element
print(list_a[-1]) //Output : 9

To find the length of a list we use len().

list_a=[1,3,4,'True','Hello',9]
print(len(list_a))  //Output : 6

We can also Iterate over a list to find an element from the list or print the elements from the list.

list_a=[1,3,4,True,'Hello',9]
for item in list_a:
     print(item)

//Output :
1
3
4
True
Hello
9

Similar to Strings, ' + ' operator concatenates two lists.

list_a = [1,2,3]
list_b = [4,5,6]
list_c = list_a + list_b
print(list_c)

//Output:
[1,2,3,4,5,6]

We can add new elements to a list either using an 'append' keyword or using a compound assignment operator.

list_a=[1,2,3]
list_a.append(4)
print(list_a)  //Output : [1,2,3,4]
list_a += [5]
print(list_a)  //Output : [1,2,3,4,5]

List Slicing

In order to obtain some part of list we use list slicing.

list_a=[1,2,3,4,5,6,7]
//To obtain first 4 elements from List.
new_list=list_a[0:4]   //Here element at index 4( '5' in list_a) is exclusive
print(new_list)     //Output : [1,2,3,4]

Note :- List slicing will always return a List even if we are accessing a single value.

list_a=[1,2,3]
item1=list_a[0]
item2=list_a[0:1]
//In the above case item1 returns single value whereas item2 returns a list.
print(item1)  //Output : 1
print(item2) //Output : [1]

In order to obtain elements of list in a particular order we can use extended slicing with a step(Default value is 1)

list_a=[0,1,2,3,4,5,6,7,8,9,10]
//We can get alternate numbers in above list with step 2.
print(list_a[ : :2])

//Output :
[0, 2, 4, 6, 8, 10]

Lists are Mutable. As discussed earlier we can update the elements of a list at any position within the given range i.e if the length of list is 4 we can update elements till list[3] as it is the last element.

list_a=[1,2,3,4]
list_a[3]=5
print(list_a)  //Output : [1,2,3,5]
list_a[4]=6 //Output : IndexError: list assignment index out of range

In the above case we have seen how a data Structure of mutable type behaves when we update a value. Now let us see how immutable type works. As we know strings are immutable. Let us try assigning a value to string through indexing.

str_a= "Hello World"
print(str_a[0])  //Output : H
str_a[0]= "T"
print(str_a) //Output : TypeError: 'str' object does not support item assignment

Modifying a List

Whatever that can be assigned to variable is called Object and whenever an object is created in Python it will be assigned with a unique Id which differs each time the program is executed. To find Id of an object we use id().

a=[1,2,3]
print(id(a)) //Output:139681468705344

You might be thinking what's the point of this Id ? I'm getting into it.

  • Whenever we assign an existing list to a variable then both the variables point towards same list and so they will have same Id.
list_a=[1,2,3]
list_b=list_a
print(id(list_a))    //Output: 140035591248384
print(id(list_b))   //Output: 140035591248384
  • As list_a and list_b are pointing towards same Object , if we change an element in any list , the change occurs in the other list as well.
list_a=[1,2,3]
list_b=list_a
list_b[2]=4
print(list_b) //Output: [1,2,4]
print(list_a) //Output: [1,2,4]
  • But if we perform an assignment to any list then it will reference a new object and there won't be any change in the other list.
list_a=[1,2,3]
list_b=list_a
list_a=[4,5,6]
print(list_b) //Output: [1,2,3]
print(list_a) //Output: [4,5,6]
  • When we perform a compound assignment then the existing list will be updated instead of creating a new object(list)
list_a=[1,2,3]
list_b=list_a
list_a+=[4,5,6]
print(list_b)  //Output : [1,2,3,4,5,6]
print(list_a)  //Output : [1,2,3,4,5,6]
  • If a list has mutable object as its element(list contains another list),updating the mutable object will effect values in list.
list_a=[2,4]
list_b=[0,1,list_a]
print(list_b)  //Output : [0,1,[2,4]]
list_a[1]=3
print(list_b)  //Output : [0,1,[2,3]]

If you've reached till here I suppose you've gone through the page. I thought of writing what I have learned so far in Lists and will be writing about other data structures like tuples, sets and dictionaries in upcoming blogs. You can always drop your comments so that I can learn things Which I've missed/committed errors.

Thank you.......