By: Ashley J in Python Tutorials on 2017年09月27日 [フレーム]
In Python, a list is a mutable, ordered sequence of items. Here are some of the commonly used operations that can be performed on lists:
Creating a list: To create a list, we can use square brackets [] or the list() constructor.
Example:
my_list = [1, 2, 3, 4, 5] my_list = list(range(1, 6))
Accessing list elements: We can access the elements of a list using their index.
Example:
my_list = [1, 2, 3, 4, 5] print(my_list[0]) # Output: 1 print(my_list[-1]) # Output: 5
Modifying list elements: We can modify the elements of a list using their index.
Example:
my_list = [1, 2, 3, 4, 5] my_list[0] = 6 print(my_list) # Output: [6, 2, 3, 4, 5]
Adding elements to a list: We can add elements to a list using the append() method or the + operator.
Example:
my_list = [1, 2, 3] my_list.append(4) print(my_list) # Output: [1, 2, 3, 4] my_list = [1, 2, 3] my_list = my_list + [4] print(my_list) # Output: [1, 2, 3, 4]
Removing elements from a list: We can remove elements from a list using the remove() method or the del keyword.
Example:
my_list = [1, 2, 3, 4, 5] my_list.remove(3) print(my_list) # Output: [1, 2, 4, 5] my_list = [1, 2, 3, 4, 5] del my_list[2] print(my_list) # Output: [1, 2, 4, 5]
Slicing a list: We can extract a subset of a list using slicing.
Example:
my_list = [1, 2, 3, 4, 5] print(my_list[1:3]) # Output: [2, 3]
Finding the length of a list: We can find the number of elements in a list using the len() function.
Example:
my_list = [1, 2, 3, 4, 5] print(len(my_list)) # Output: 5
Sorting a list: We can sort a list using the sort() method.
Example:
my_list = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5] my_list.sort() print(my_list) # Output: [1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9]
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
Most Viewed Articles (in Python )
Retrieve Twitter posts and comments using Python
Python program to get location meta data from an image
How to install Jupyter in Ubuntu and make it accessible through Apache Reverse Proxy
Installing gedit for python programming in Windows
Latest Articles (in Python)
Python program to get location meta data from an image
Retrieve Twitter posts and comments using Python
How to install Jupyter in Ubuntu and make it accessible through Apache Reverse Proxy
Python Basics - Setting up your Python Development Environment
Schwartzian Transform in python
Multidimensional list (array) in python
Perl's chomp() equivalent for removing trailing newlines from strings in python
Python program to get location meta data from an image
Retrieve Twitter posts and comments using Python
How to install Jupyter in Ubuntu and make it accessible through Apache Reverse Proxy
Python Basics - Setting up your Python Development Environment
Schwartzian Transform in python
Multidimensional list (array) in python
Perl's chomp() equivalent for removing trailing newlines from strings in python
© 2023 Java-samples.com
Tutorial Archive: Data Science React Native Android AJAX ASP.net C C++ C# Cocoa Cloud Computing EJB Errors Java Certification Interview iPhone Javascript JSF JSP Java Beans J2ME JDBC Linux Mac OS X MySQL Perl PHP Python Ruby SAP VB.net EJB Struts Trends WebServices XML Office 365 Hibernate
Latest Tutorials on: Data Science React Native Android AJAX ASP.net C Cocoa C++ C# EJB Errors Java Certification Interview iPhone Javascript JSF JSP Java Beans J2ME JDBC Linux Mac OS X MySQL Perl PHP Python Ruby SAP VB.net EJB Struts Cloud Computing WebServices XML Office 365 Hibernate