Understanding Lists in Python: A Comprehensive Guide
Table of Content:
Python Lists are just like dynamically sized arrays, declared in other languages (vector in C++ and ArrayList in Java). In simple language, a list is a collection of things, enclosed in [ ] and separated by commas.
Example of List
mylist = ["apple", "banana", "cherry"]
Creating a list
Lists are enclosed in square brackets [ ] and each item is separated by a comma.
Blank list creation
A list can be created without element
ListBlank = [ ]
Initializing a list
Passing value in list while declaring list is initializing of a list.
list1 = ['English', 'Hindi', 1997, 2000] list2 = [11, 22, 33, 44, 55 ] list3 = ["a", "b", "c", "d"]
Characteristics of Lists
The list has the following characteristics:
- The lists are ordered.
- The element of the list can access by index.
- The lists are the mutable type.
- The lists are mutable types.
- A list can store the number of various elements.
Let's check the first statement that lists are the ordered.
a = [1,2,"Rumman",4.50,"Ansari",5,6] b = [1,2,5,"Rumman",4.50,"Ansari",6] a == b
Output
False
Both lists have consisted of the same elements, but the second list changed the index position of the 5th element that violates the order of lists. When compare both lists it returns the false.
Lists maintain the order of the element for the lifetime. That's why it is the ordered collection of objects.
Code
a = [1, 2,"Rumman", 4.50,"Ansari",5, 6] b = [1, 2,"Rumman", 4.50,"Ansari",5, 6] a == b
Output
True
Access Items From A List
Positive indexing
list =[3,5,9] print(list[0]) print(list[1]) print(list[2])
Output
3 5 9
Negative indexing
print(list[-1]) print(list[-2]) print(list[-3])
Output
9 5 3
Iterating/Traversing Through a List
A list can be iterated by using a for - in loop. A simple list containing four strings, which can be iterated as follows.
Code
list = ["John", "David", "James", "Jonathan"] for i in list: # The i variable will iterate over the elements of the List and contains each element in each iteration. print(i)
Output
John David James Jonathan
Slicing of A List
List elements can be accessed in subparts.
list =['I','N','D','I','A'] print(list[0:3]) print(list[3:]) print(list[:])
Output
['I', 'N', 'D'] ['I', 'A'] ['I', 'N', 'D', 'I', 'A']
Example 1: Write a program to find the sum of the element in the list.
list1 = [3,4,5,9,10,12,24] sum = 0 for i in list1: sum = sum+i print("The sum is:",sum)
Output
The sum is: 67
Example 2: Write the program to find the lists consist of at least one common element.
list1 = [1,2,3,4,5,6] list2 = [7,8,9,2,10] for x in list1: for y in list2: if x == y: print("The common element is:",x)
Output
The common element is: 2
List Comprehensions
Alternative to for loops.
More concise, readable, efficient and mimic functional programming style.
-
Used to:
Apply a method to all or specific elements of a list, and
Filter elements of a list satisfying specific criteria.
Example
x = [6, 3, 1] y = [ i**2 for i in x ] # List Comprehension expression print(y) # -> [36, 9, 1]
List Points to remember
- A list is a standard data type of Python that can store a sequence of values belonging to any type.
- The lists are depicted through square brackets.
- These are mutable (i.e. modifiable), you can change elements of a list in place.
- Lists store a reference at each index.
- We can index, slice and access individual list elements.
- len (L) returns the number of items in the list L.Membership operators in and not in can be used with list.
- To join two lists, use `+’ (concatenation) operator.
- L [start: stop] creates a list slice with starting index as start till stop as stopping index but excluding stop.
- List manipulation functions are
append(), insert(), extend(),sort(), remove(), reverse() and pop().