The list data structure in Python provides an excellent method for
storing arrays of objects in Python. A programmer can construct lists of
any data type. This is easy to imagine if you can think of a shopping
list where you have a list of items to buy, except that you probably
have each item on a separate line in your shopping list whereas in
Python you put commas in between them.
Python vs. Perl: lists
A list in Python is like an array in Perl. In Perl, variables that store arrays always start with the @ character; in Python, variables can be named anything, and Python keeps track of the datatype internally.
Python vs. Java: lists
A list in Python is much more than an array in Java.
Lists are similar to strings, which are ordered sets of characters, except that the elements of a list can have any type.
Creating List :
There are several ways to create a new list; the simplest is to enclose the elements
in square brackets ([ and ]):
>>> test = ["a", "b", "hello", "z", "world"]
>>> test
['a', 'b', 'hello', 'z', 'world']
>>> test[0]
'a'
>>> test[4]
'world'
List Operations :
The three standard sequence operations (‘+’, ‘*’, ‘[]’) can be performed with list, as well as other sequences like tuple and string.
The ‘+’ operator creates a new list as the concatenation of the arguments.
>>> a = [1, 2, 3]
>>> b = [4, 5, 6]
>>> c = a + b
>>> print c
[1, 2, 3, 4, 5, 6]
Similarly, the * operator repeats a list a given number of times:
>>> [0] * 4
[0, 0, 0, 0]
>>> [1, 2, 3] * 3
[1, 2, 3, 1, 2, 3, 1, 2, 3]
List Built-in Functions :
1. len(): For lists, this function returns the number of items.
2. max(): For lists, this function returns the maximum item.
3. sorted():Iterate through the list in sorted order.
4. reversed(): Iterate through the list in reverse order.
List Methods :
A list object has a number of member methods. These can be grouped arbitrarily into transformations, which change the list, and information, which returns a fact about a list.
The list transformation functions update a list object like :
append(object): Update list by appending object to end of the list.
extend(list) : Extend list by appending list elements.
insert(index, object): Update list l by inserting object before position index.
pop([index=-1]):Remove and return item at index
remove(value) :Remove first occurrence of value from list.
reverse() : Reverse the items of the list.
sort([key], [reverse=False]) : Sort the items of the list.
For example :
>>> portList = []
>>> portList.append(21)
>>> portList.append(80)
>>> portList.append(443)
>>> portList.append(25)
>>> print portList
[21, 80, 443, 25]
>>> portList.sort()
>>> print portList
[21, 25, 80, 443]
>>> portList.pop(2)
80
>>> print portList
[21, 25, 443]
Lists are mutable : Unlike strings, lists are mutable, which means we can change their elements. Using the bracket operator on the left side of an assignment, we can update one of the elements:
>>> fruit = ["banana", "apple", "quince"]
>>> fruit[0] = "pear"
>>> fruit[-1] = "orange"
>>> print fruit
[’pear’, ’apple’, ’orange’]
If you like this post or have any question, please feel free to comment!
Python vs. Perl: lists
A list in Python is like an array in Perl. In Perl, variables that store arrays always start with the @ character; in Python, variables can be named anything, and Python keeps track of the datatype internally.
Python vs. Java: lists
A list in Python is much more than an array in Java.
Lists are similar to strings, which are ordered sets of characters, except that the elements of a list can have any type.
Creating List :
There are several ways to create a new list; the simplest is to enclose the elements
in square brackets ([ and ]):
>>> test = ["a", "b", "hello", "z", "world"]
>>> test
['a', 'b', 'hello', 'z', 'world']
>>> test[0]
'a'
>>> test[4]
'world'
List Operations :
The three standard sequence operations (‘+’, ‘*’, ‘[]’) can be performed with list, as well as other sequences like tuple and string.
The ‘+’ operator creates a new list as the concatenation of the arguments.
>>> a = [1, 2, 3]
>>> b = [4, 5, 6]
>>> c = a + b
>>> print c
[1, 2, 3, 4, 5, 6]
Similarly, the * operator repeats a list a given number of times:
>>> [0] * 4
[0, 0, 0, 0]
>>> [1, 2, 3] * 3
[1, 2, 3, 1, 2, 3, 1, 2, 3]
List Built-in Functions :
1. len(): For lists, this function returns the number of items.
2. max(): For lists, this function returns the maximum item.
3. sorted():Iterate through the list in sorted order.
4. reversed(): Iterate through the list in reverse order.
List Methods :
A list object has a number of member methods. These can be grouped arbitrarily into transformations, which change the list, and information, which returns a fact about a list.
The list transformation functions update a list object like :
append(object): Update list by appending object to end of the list.
extend(list) : Extend list by appending list elements.
insert(index, object): Update list l by inserting object before position index.
pop([index=-1]):Remove and return item at index
remove(value) :Remove first occurrence of value from list.
reverse() : Reverse the items of the list.
sort([key], [reverse=False]) : Sort the items of the list.
For example :
>>> portList = []
>>> portList.append(21)
>>> portList.append(80)
>>> portList.append(443)
>>> portList.append(25)
>>> print portList
[21, 80, 443, 25]
>>> portList.sort()
>>> print portList
[21, 25, 80, 443]
>>> portList.pop(2)
80
>>> print portList
[21, 25, 443]
Lists are mutable : Unlike strings, lists are mutable, which means we can change their elements. Using the bracket operator on the left side of an assignment, we can update one of the elements:
>>> fruit = ["banana", "apple", "quince"]
>>> fruit[0] = "pear"
>>> fruit[-1] = "orange"
>>> print fruit
[’pear’, ’apple’, ’orange’]
If you like this post or have any question, please feel free to comment!
No comments:
Post a Comment