List Comprehension
List Comprehension
List Introduction
kPython 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.
The list is a sequence data type which is used to store the collection of
data. Tuples and String are other types of sequence data types.
Python3
Output
Lists are the simplest containers that are an integral part of the Python language.
Lists need not be homogeneous always which makes it the most powerful tool in
Python. A single list may contain DataTypes like Integers, Strings, as well as
Objects. Lists are mutable, and hence, they can be altered even after their
creation.
Articles
Example:
Python3
Videos
# Creating a List
Quiz
List = []
print("Blank List: ")
print(List)
Output
Blank List:
[]
List of numbers:
[10, 20, 14]
List Items:
Menu Geeks
Geeks
Articles Read
0 of 6 Complete. (0%)
https://practice.geeksforgeeks.org/batch/Datasci-4/track/da-python-data-structure/article/Njc2Mg%3D%3D 2/15
3/24/23, 8:37 PM Practice | GeeksforGeeks | A computer science portal for geeks
Dash
Accessing elements from the List
In order to access the list items refer to the index number. Use the index operator [ ]
to access an item in a list. The index mustArticles
be an integer. Nested lists are accessed
using nested indexing.
Example 1: Accessing elements from listVideos
Python3
Problems
Output
Python3
Articles Read
# accessing
(0%) an element from the
0 of 6 Complete.
# Multi-Dimensional List using
https://practice.geeksforgeeks.org/batch/Datasci-4/track/da-python-data-structure/article/Njc2Mg%3D%3D 3/15
3/24/23, 8:37 PM Practice | GeeksforGeeks | A computer science portal for geeks
Videos
Output
Quiz
Negative indexing
In Python, negative sequence indexes represent positions from the end of the array.
Instead of having to compute the offset as in List[len(List)-3], it is enough to just
write List[-3]. Negative indexing means beginning from the end, -1 refers to the last
item, -2 refers to the second-last item, etc.
Python3
Output
Menu
Accessing element using negative indexing
Geeks
For
Articles Read
0 of 6 Complete. (0%)
https://practice.geeksforgeeks.org/batch/Datasci-4/track/da-python-data-structure/article/Njc2Mg%3D%3D 4/15
3/24/23, 8:37 PM Practice | GeeksforGeeks | A computer science portal for geeks
Dash
Getting the size of the Python list
Python len() is used to get the length of the list.
Articles
Python3
Videos
# Creating a List
List1 = []
Problems
print(len(List1))
Output
0
3
Elements can be added to the List by using the built-in append() function. Only one
element at a time can be added to the list by using the append() method, for the
addition of multiple elements with the append() method, loops are used. Tuples
can also be added to the list with the use of the append method because tuples
are immutable. Unlike Sets, Lists can also be added to the existing list with the use
of the append() method.
Example:
Python3
Menu
# Python program to demonstrate
# Addition of elements in a List
Articles Read
0 of 6 Complete. (0%) a List
# Creating
https://practice.geeksforgeeks.org/batch/Datasci-4/track/da-python-data-structure/article/Njc2Mg%3D%3D 5/15
3/24/23, 8:37 PM Practice | GeeksforGeeks | A computer science portal for geeks
List = [] Dash
# Addition of Elements
# in the List
Videos
List.append(1)
List.append(2)
List.append(4)
Problems
print("\nList after Addition of Three elements: ")
print(List)
Quiz
# Adding elements to the List
# using Iterator
for i in range(1, 4):
List.append(i)
print("\nList after Addition of elements from 1-3: ")
print(List)
Output
Articles Read
List after Addition of a Tuple:
0 of 6 Complete. (0%)
[1, 2, 4, 1, 2, 3, (5, 6)]
https://practice.geeksforgeeks.org/batch/Datasci-4/track/da-python-data-structure/article/Njc2Mg%3D%3D 6/15
3/24/23, 8:37 PM Practice | GeeksforGeeks | A computer science portal for geeks
Dash
List after Addition of a List:
[1, 2, 4, 1, 2, 3, (5, 6), ['For', 'Geeks']]
Articles
Parameters:
index: the index at which the element has to be inserted.
element: the element to be inserted in the list.
Returns: Does not return any value.
Python3
Problems
Output
Courses
['Geeks', 'For', 'Geeks']
Get Hired
2249
Example 2: Inserting an Element into the List
Scholarship
Contests
Articles Read
0 of 6 Complete. (0%)
https://practice.geeksforgeeks.org/batch/Datasci-4/track/da-python-data-structure/article/Njc2Mg%3D%3D 7/15
3/24/23, 8:37 PM Practice | GeeksforGeeks | A computer science portal for geeks
Dash
list1 = [ 1, 2, 3, 4, 5, 6, 7 ]
Articles
Problems
Output
[1, 2, 3, 4, 10, 5, 6, 7]
Quiz
count() method
Python List count() method returns the count of how many times a given object
occurs in a List.
Syntax:
Syntax: list_name.count(object)
Parameters:
object: is the item whose count is to be returned.
Returns: Returns the count of how many times object occurs in the list.
Exception:
TypeError: Raises TypeError If more than 1 parameter is passed
in count() method.
Example:
Menu Python3
Output
3 Articles
Videos
del keyword
In python del is a keyword and remove(), pop() are in-built methods. The purpose
Problems
of these three are the same but the behavior is different remove() method delete
values or object from the list using value and the del and pop() deletes values or
object from the list using an index.The del
Quizkeyword deletes any variable, or list of
values from a list.
Syntax:
Example:
Python3
# assign list
numbers = [1, 2, 3, 2, 3, 4, 5]
# use del
del numbers[2]
# display list
print(numbers)
# use del
del numbers[-1]
Articles Read
# use del
0 of 6 Complete. (0%)
del numbers[0]
https://practice.geeksforgeeks.org/batch/Datasci-4/track/da-python-data-structure/article/Njc2Mg%3D%3D 9/15
3/24/23, 8:37 PM Practice | GeeksforGeeks | A computer science portal for geeks
Dash
# display list
print(numbers)
Articles
Output
Videos
[1, 2, 2, 3, 4, 5]
[1, 2, 2, 3, 4]
[2, 2, 3, 4] Problems
Quiz
remove() method
The remove() method removes the first matching value from the list.
Syntax:
list_name.remove(value)
Example:
Python3
# assign list
numbers = [1, 2, 3, 2, 3, 4, 5]
# use remove()
numbers.remove(3)
# display list
print(numbers)
# use remove()
numbers.remove(2)
Menu
# display list
print(numbers)
Articles Read
0 of 6 Complete. (0%)
# use remove()
https://practice.geeksforgeeks.org/batch/Datasci-4/track/da-python-data-structure/article/Njc2Mg%3D%3D 10/15
3/24/23, 8:37 PM Practice | GeeksforGeeks | A computer science portal for geeks
numbers.remove(5) Dash
# display list
Articles
print(numbers)
Videos
Output
[1, 2, 2, 3, 4, 5]
[1, 2, 3, 4, 5] Problems
[1, 2, 3, 4]
Quiz
pop() method
The pop() method like the del keyword deletes value at a particular index. But the
pop() method returns deleted value from the list.
Syntax:
list_name.pop(index)
Python3
# assign list
numbers = [1, 2, 3, 2, 3, 4, 5]
# use remove()
numbers.pop(3)
# display list
print(numbers)
# use remove()
numbers.pop(-1)
Menu
# display list
print(numbers)
Articles Read
0 of 6 Complete.
# use (0%)
remove()
https://practice.geeksforgeeks.org/batch/Datasci-4/track/da-python-data-structure/article/Njc2Mg%3D%3D 11/15
3/24/23, 8:37 PM Practice | GeeksforGeeks | A computer science portal for geeks
numbers.pop(0) Dash
# display list
Articles
print(numbers)
Videos
Output
[1, 2, 3, 3, 4, 5]
[1, 2, 3, 3, 4] Problems
[2, 3, 3, 4]
Quiz
max() method
This function is used to compute the maximum of the values passed in its argument
and the lexicographically largest value if strings are passed as arguments.
Syntax:
max(a,b,c,..,key,default)
Parameters :
a,b,c,.. : similar type of data.
key : key function where the iterables are passed and comparison is perf
ormed
default : default value is passed if the given iterable is empty
Return Value :
Returns the maximum of all the arguments.
Exceptions :
Returns TypeError when conflicting types are compared.
Example:
Python3
Output
Videos
min() method
This function is used to compute the minimum
Problems of the values passed in its argument
and lexicographically smallest value if strings are passed as arguments.
Quiz
Syntax:
min(a,b,c,.., key,default)
Parameters :
a,b,c,.. : similar type of data.
key : key function where the iterables are passed and comparison is perf
ormed
default : default value is passed if the given iterable is empty
Return Value :
Returns the minimum of all the arguments.
Exceptions :
Returns TypeError when conflicting types are compared.
Example:
Python3
Output
Menu
Minimum of 4,12,43.3,19 and 100 is : 4
Articles Read
0 of 6 Complete. (0%)
https://practice.geeksforgeeks.org/batch/Datasci-4/track/da-python-data-structure/article/Njc2Mg%3D%3D 13/15
3/24/23, 8:37 PM Practice | GeeksforGeeks | A computer science portal for geeks
Dash
sort() function
Python list sort() function can be used to sort a List in ascending, descending, or
user-defined order. Articles
Python3
numbers = [1, 3, 4, 2]
# Sorting list of Integers in ascending
numbers.sort()
print(numbers)
Output
[1, 2, 3, 4]
https://practice.geeksforgeeks.org/batch/Datasci-4/track/da-python-data-structure/article/Njc2Mg%3D%3D 14/15
3/24/23, 8:37 PM Practice | GeeksforGeeks | A computer science portal for geeks
Dash
Syntax: list_name.reverse()
Parameters: There are no parameters.
Articles
Returns: The reverse() method does not return any value but reverses
the given object from the list.
Videos
Here we are reversing the list using the list reverse() function in Python.
Problems
Python3
Quiz
# Python3 program to demonstrate the
# use of reverse method
# a list of numbers
list1 = [1, 2, 3, 4, 1, 2, 6]
list1.reverse()
print(list1)
# a list of characters
list2 = ['a', 'b', 'c', 'd', 'a', 'a']
list2.reverse()
print(list2)
Output
[6, 2, 1, 4, 3, 2, 1]
['a', 'a', 'd', 'c', 'b', 'a']
Mark as Read
Report An Issue
If you are facing any issue on this page. Please let us know.
Menu
Articles Read
0 of 6 Complete. (0%)
https://practice.geeksforgeeks.org/batch/Datasci-4/track/da-python-data-structure/article/Njc2Mg%3D%3D 15/15