15.python Lists
15.python Lists
Home /
Tutorial /
Python Lists
Python Lists
By Manoj 9.3 K Views 22 min read Updated on March 9, 2022
In this module of the Python tutorial, we will learn in detail about the list data type in Python. We will further learn how to create
lists, including multi-dimensional lists, how to access lists, and Python list comprehensions, and toward the end of this module
we will also learn about various operations in Python list data type.
Python Tutorial
Python Variables -
Constant, Global & Static
Variables
Numbers in Python
String in Python
Python Lists
Tuple in Python
Python Sets
Python Dictionary
Python Operators
Type conversion in
Python
Python If Else
Statements
Python Functions -
Define & Call a Functions
in Python
Lambda Function in
Python
Python Built in
Functions with Examples
Python Arrays
Python Classes and
Objects
Python Modules
Python Dates
Python JSON
Python RegEx
PIP Python
Exception Handling in
Python
Enumerate Function in
List in Python
Lists are Python’s most flexible ordered collection object type. It can also be referred to as a sequence that is an ordered
collection of objects that can host objects of any data type, such as Python Numbers, Python Strings, and nested lists as
well. Lists are one of the most used and versatile Python Data Types. In this module, we will learn all about lists in order to
get started with them.
Following is the list of all topics that are going to be covered in this module.
Creating_Lists_in_Python
Creating Multi-dimensional Lists in Python
Python List Comprehension
Python Lists Extension
Accessing Lists in Python
Length of List in Python
Linked List in Python
List to String in Python
Common_List_Operations_in_Python
Unlike strings, lists can contain any sort of object: numbers, strings, and even other lists. Python lists are:
Ordered collections of arbitrary objects
Accessed by offset
Arrays of object references
Of variable length, heterogeneous, and arbitrarily nestable
Of the category, mutable sequence
Data types in which elements are stored in the index basis with starting index as 0
Enclosed between square brackets ‘[]’
Example:
list1 = [1,2,3,4,5]
print(init_list)
Output:
[0, 0, 0]
print(two_dim_list)
Output:
print(two_dim_list)
Output:
List = [1,2,3,4,5]
print(List1)
Output:
[0, 1, 2, 3, 4]
Output:
Example 2:
list_fruit = [“Apple”,”Mango”,”Banana”,”Avocado”]
print(first_letters)
Output:
List Extension
Python allows lists to resize in many ways. We can do that just by adding two or more of them.
Example:
two_dim[0][2] = 1
print(two_dim)
Output:
extend():
Alternately, we can do extension by using the extend() method. See the following example:
L1 = [‘a’, ‘b’]
L2 = [‘c’, ‘d’]
L1.extend(L2)
print(L1)
Output:
Learn more about Lists from our blog on Python List Comprehension.
append():
Next, we can append a value to a list by calling the append() method. See the following example:
L1 = [‘a’, ‘b’]
L2 = [‘c’, ‘d’]
L1.extend(L2)
print(L1)
Output:
Example:
list1 = [1,2,3,4,5]
print(list1[-3])
Output:
Length of List
In python, there is a built-in method called, len() that helps you get the number of items in a list. It can also be used for
arrays, tuples, dictionaries etc. The function takes a list as the argument and returns its length.
Python does not have linked lists in its standard library so it is implemented using the concept of nodes.
Each of the data elements is connected to other data elements using pointers. Linked lists contain a link element called
first. Each link carries two fields, a data field and a link field called next, which links each element to the next element. The
last link carries a null link, marking that it is the end of the linked list.
Check out how Intellipaat is helping people get Python training in the easiest way. And also, we are providing trainees
with a free guide to all Python interview questions.
List to String
A list can be converted to a string using the join() method.
def ListToString(a):
s1 = “ ”
return(s1.join(a))
a = [‘Intellipaat’,‘Python’,‘tutorial’]
print(ListToString(a)
list1[2:4]
output:
[3, 4]
list1[2:-1]
output:
[3, 4]
list1[:2]
output:
[1, 2]
Looking for Data Science with Python training All-in-1 Combo Training? Enroll now!
list1 = [1,2,3,4,5]
print(element)
Output:
Example: If you have a list of programming languages, and based on that, you have to create a new list, containing just the
languages with the letter ‘a’ in their name. Without list comprehension, you would need to write a for statement with the
condition inside, whereas, using list comprehension, all that can be done with just one line of code:
print(newlist)
Example:
print(list1)list1.insert(0,33)
print(list1)list1.insert(6,29)
print(list1)
Output:
[1, 2, 3, 4, ‘number’]
list1 = [1,2,3,4,5]
del list1[2]
list2 = [1,2,3,4,5]
list2.remove(4)
print(list2)
list3 = [1,2,3,4,5]
print(list3.pop(1))
print(list3)
Output:
[1, 2, 4, 5][1, 2, 3, 5]
[1, 3, 4, 5]
mylist = list(dict.fromkeys(mylist))
output:
[“a”, “b”,”c”,”d”]
lst.reverse()
print(lst)
output:
Enroll yourself in Online Python Training in Sydney and give a head-start to your career in Python Programming!
list1.sort()
list1 = [1,3,2,4,5,9,6]
list1.sort()
print(list1)
output:
[1, 2, 3, 4, 5, 6, 9]
list1 = [1,3,2,4,5,9,6]
list1.sort(reverse=True)
print(list1)
output:
[9, 6, 5, 4, 3, 2, 1]
Method Description
With this, we come to the end of this module in Python Tutorial. Now, if you’re interested to know why Python is the most
preferred language for Data Science, you can go through this Python Data Science tutorial.
Previous Next
Course Schedule