Python |
||
Python - List
List in Python is similar to an array in any other language, but it has much more methods (features) to handle elements (item) of the list.
A Python list is a built-in data structure that stores a mutable, ordered sequence of elements. Lists are versatile and can hold elements of different data types, including other lists, which makes them suitable for a wide range of applications. Since lists are mutable, their elements can be added, removed, or modified after the list is created.
To create a list, you can use square brackets [] and separate the elements with commas. An empty list can be created by using an empty pair of square brackets []. The elements in a list can be of any data type, such as integers, floats, strings, or even other lists and custom objects.
One of the main features of lists is that they are ordered, which means that the elements maintain their position within the list. You can access individual elements of a list using their index, starting with 0 for the first element, 1 for the second, and so on. Negative indices can be used to access elements from the end of the list, with -1 referring to the last element, -2 to the second last, and so on.
Python provides various built-in methods and functions to manipulate lists, such as:
Differences among Array, Set, Dictionary
Arrays, sets, and dictionaries are all collection data structures in Python, but they have some key differences in terms of their properties and use cases. Here's a summary of their main differences:
Arrays:
Sets:
Dictionaries:
Example
Followings are examples showing various operations and usage of lists.
NOTE 1 : All the examples in this page are written in Python 3.x. It may not work if you use Pyton 2.x NOTE 2 : All the examples in this page are assumed to be written/run on Windows 7 unless specifically mentioned. You MAY (or may not) need to modify the syntax a little bit if you are running on other operating system.
Examples :
|
||