Python Lists

Er Ravindra Pawadia
4 min readOct 14, 2020

List is a collection data structure which is ordered and changeable. If you want to represent a group of individual objects as a single entity where insertion order preserved and duplicates are allowed, then you should go for List.

  • Insertion order preserved.
  • Duplicate objects are allowed
  • Heterogeneous objects are allowed.
  • Growable in nature
  • Values should be enclosed within square brackets
  • List is dynamic because based on our requirement we can increase the size and decrease the size.
  • In List the elements will be placed within square brackets and with comma separator.

You can differentiate duplicate elements by using index and you can preserve insertion order by using index. Hence index will play very important role.

Python supports both positive and negative indexes.

  • Positive index means from left to right.
  • Negative index means right to left.

Note : List objects are mutable.i.e we can change the content.

Creation of List Objects

We have different methods to create a list.

list=["Alphs",10,20,30,40]list=eval(input("Enter List:")) 
print(list)
print(type(list))

You can use any Python IDE (Integrated Development Environment) Editors. PyCharm is widely popular. You can install PyCharm in your machine. Paste above lines in in your favourite editor.

Enter List:[10,20,30,40] 
[10, 20, 30, 40]
<class 'list'>
l=list(range(0,10,2))
print(l)
print(type(l))
Output
[0, 2, 4, 6, 8]
<class 'list'>

Another example is

s="Learning Python is very very easy !!!" 
l=s.split()
print(l)
print(type(l))
Output ['Learning', 'Python', 'is', 'very', 'very', 'easy', '!!!']
<class 'list'>

Note : Sometimes we can take list inside another list, such type of lists are called nested lists.

[10,20,[30,40]] ---> Nested List

Accessing Elements of List

We can access elements of the list either by using index or by using slice operator(:).

List follows zero based index ie. index of first element is zero. List supports both +ve and -ve indexes.

  • +ve index meant for Left to Right
  • -ve index meant for Right to Left

list=[10,20,30,40,50,60]

By Using Slice Operator

The Basic syntax of slice operator as follows:

list2= list1[start:stop:step]

  • Start -> It indicates the index where slice has to start (default value is 0)
  • Stop -> It indicates the index where slice has to end (default value is max allowed index of list ie. length of the list)
  • Step -> Increment value (default value is 1)

Change Item Value of a List (List vs Mutability)

Once we creates a List object, we can modify its content. Hence List objects are mutable.

n=[10,20,30,40] print(n) ==> [10, 20, 30, 40] n[1]=777 print(n) ==> [10, 777, 30, 40]

Traversing the Elements of Lists

The sequential access of each element in a list is called traversal.

We can use for loop in lists as well.

To display only even Numbers

We can get only even numbers from a list using for loop.

n=[0,1,2,3,4,5,6,7,8,9,10] 
for n1 in n:
if n1%2==0:
print(n1)
Output

0
2
4
6
8
10

Important functions of List

To get information about list, we can use below functions,

This function returns the number of elements present in the list.

list=[10,20,30,40] 
print(len(list)) ==> 4

It returns the number of occurrences of specified item in the list.

list = [1,2,2,2,2,3,3] 
print(list.count(1)) ==> 1
print(list.count(2)) ==> 4
print(list.count(3)) ==> 2
print(list.count(4)) ==> 0

It returns the index of first occurrence of the specified item.

list = [1,2,2,2,2,3,3] 
print(list.index(1)) ==> 0
print(list.index(2)) ==> 1
print(list.index(3)) ==> 5
print(list.index(4)) ==> ValueError: 4 is not in list

If the specified element not present in the list then we will get ValueError.

Manipulating elements of a List

We can use append() function to add item at the end of the list.

You can insert item at specified index position using index() function.

If the specified index is greater than maximum index then element will be inserted at last position. If the specified index is smaller than minimum index then element will be inserted at first position.

We can add all items of one list to another list using extend() function.

list1.extend(list2) -> all items present in list2 will be added to list1

We can use this function to remove specified item from the list. If the item present multiple times then only first occurrence will be removed.

If the specified item not present in list then we will get ValueError.

It removes and returns the last element of the list. This is only function which manipulates list and returns some element.

If the list is empty then pop() function raises IndexError

n=[] 
print(n.pop()) ==> IndexError: pop from empty list

Ordering elements of List

We can use to reverse() order of elements of list.

In list by default insertion order is preserved. If want to sort the elements of list according to default natural sorting order then we should go for sort() method.

  • For numbers ==> default natural sorting order is Ascending Order
  • For Strings ==> default natural sorting order is Alphabetical Order
list1=[20,5,15,10,0] 
list1.sort()
print(list1) # [0,5,10,15,20]
list2 = ["Dog","Banana","Cat","Apple"]
list2.sort()
print(list2) # ['Apple','Banana','Cat','Dog']

We can use clear() function to remove all elements of List.

list=[10,20,30,40] 
print(list)
list.clear()
print(n)
Output [10, 20, 30, 40] []

We can use + to concatenate or add two lists into a single list.

list1 = [10,20,30] 
list2 = [40,50,60]
list = list1 + list2
print(list1)
Output [10,20,30,40,50,60]

Functions used with List

Python has a set of built-in methods that you can use with lists. There are some limitations on medium platform. For complete article please visit https://thecodecloud.in/category/python-datascience-ai-ml/

Read Also : How to Install Pycharm IDE for Windows and Create First Python Project

Originally published at https://thecodecloud.in on October 14, 2020.

--

--

Er Ravindra Pawadia

Hi Guys, This is Ravi. I am AWS and Oracle Certified Solution Architect Associate. I love to write technical blogs on my blogging site https://thecodecloud.in .