List 1
List 1
11.2.3.1create a list
11.2.3.2organize the output of a string using the split() and join() methods
11.2.2.1perform access to the elements of strings, lists, tuples
11.2.3.3apply functions and methods of processing lists
11.4.3.2solve applied problems from various subject areas
11.2.3.6 determine the difference between different data structures
mylist = ["apple", "banana", "cherry"]
List
Lists are used to store multiple items in a single variable.
Lists are one of 4 built-in data types in Python used to store collections of
data, the other 3 are Tuple, Set, and Dictionary, all with different qualities and
usage.
Ordered
When we say that lists are ordered, it means that the items have a defined order, and that
order will not change.
If you add new items to a list, the new items will be placed at the end of the list.
Note: There are some list methods that will change the order, but in general: the order of
the items will not change.
Changeable
The list is changeable, meaning that we can change, add, and remove items in a list after it has been
created.
Allow Duplicates
Since lists are indexed, lists can have items with the same value:
There are four collection data types in the Python programming language:
•List is a collection which is ordered and changeable. Allows duplicate members.
•Tuple is a collection which is ordered and unchangeable. Allows duplicate members.
•Set is a collection which is unordered, unchangeable*, and unindexed. No duplicate members.
•Dictionary is a collection which is ordered** and changeable. No duplicate members.
Python - Access List Items
Print the second item of the list: Note: The first item has index 0.
thislist = ["apple", "banana", "cherry"]
print(thislist[1])
Negative Indexing
Negative indexing means start from the end -1 refers to the last item, -2 refers to the
second last item etc.
thislist = ["apple", "banana", "cherry"]
print(thislist[-1])
Range of Indexes
You can specify a range of indexes by specifying where to start and where to end the range.
When specifying a range, the return value will be a new list with the specified items.
Change the values "banana" and "cherry" with the values "blackcurrant" and "watermelon":
thislist = ["apple", "banana", "cherry", "orange", "kiwi", "mango"]
thislist[1:3] = ["blackcurrant", "watermelon"]
print(thislist)
Note: The length of the list will change when the number of items inserted does not match the
number of items replaced.
Python - Add List Items To append elements from another list to the
current list, use the extend() method.
To add an item to the end of the list, use
the append() method:
Add the elements of tropical to thislist:
thislist = ["apple", "banana", "cherry"]
thislist.append("orange") thislist = ["apple", "banana", "cherry"]
print(thislist) tropical = ["mango", "pineapple", "papaya"]
thislist.extend(tropical)
print(thislist)
Insert Items
To insert a list item at a specified index, use the insert()
method.
The insert() method inserts an item at the specified Add Any Iterable
index: The extend() method does not have to append lists, you can add any iterable
object (tuples, sets, dictionaries etc.).