Python List
Python List
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.
Python List
• Lists are created using square brackets:
E.g.
List Items
• List items are ordered, changeable, and allow
duplicate values.
• List items are indexed, the first item has index
[0], the second item has index [1] etc.
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.
Changeable
• The list is changeable, meaning that we can
change, add, and remove items in a list after it
has been created.
• Change
• Add
Changeable
• Remove
• We have four types – remove, pop, del, clear
• Del
• The remove() method removes the specified item.
• Pop
• The pop()method removes the specified index.
• If you do not specified the index, the pop() method removes
the last item.
Changeable
• Remove
• We have four types – remove, pop, del, clear
• Del
• del keyword also removes the specified index.
• The del keyword can also delete the list completely.
Changeable
• Remove
• We have four types – remove, pop, del, clear.
• Clear
• The clear() method empties the list.
• The list still remains, but it has no content.
Allow Duplicates
• Since lists are indexed, lists can have items with
the same value.
Example
Lists allow duplicate values:
List Length
• To determine how many items a list has, use the
len() function:
Example
Print the number of items in the list:
List Items - Data Types
• List items can be of any data type:
Example
String, int and boolean data types:
A list can contain different data types:
List Items - Data Types
• List items can be of any data type:
Example
String, int and boolean data types:
A list can contain different data types:
List Items - Data Types
• type()
• From Python's perspective, lists are defined as objects
with the data type 'list’:
<class 'list'>
The list() Constructor
• It is also possible to use the list() constructor
when creating a new list.
Example
• Using the list() constructor to make a List:

Lists Data Structures and Algorithm.pptx

  • 1.
  • 2.
    Python List 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.
  • 3.
    Python List • Listsare created using square brackets: E.g.
  • 4.
    List Items • Listitems are ordered, changeable, and allow duplicate values. • List items are indexed, the first item has index [0], the second item has index [1] etc.
  • 5.
    Ordered • When wesay 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.
  • 6.
    Changeable • The listis changeable, meaning that we can change, add, and remove items in a list after it has been created. • Change • Add
  • 7.
    Changeable • Remove • Wehave four types – remove, pop, del, clear • Del • The remove() method removes the specified item. • Pop • The pop()method removes the specified index. • If you do not specified the index, the pop() method removes the last item.
  • 8.
    Changeable • Remove • Wehave four types – remove, pop, del, clear • Del • del keyword also removes the specified index. • The del keyword can also delete the list completely.
  • 9.
    Changeable • Remove • Wehave four types – remove, pop, del, clear. • Clear • The clear() method empties the list. • The list still remains, but it has no content.
  • 10.
    Allow Duplicates • Sincelists are indexed, lists can have items with the same value. Example Lists allow duplicate values:
  • 11.
    List Length • Todetermine how many items a list has, use the len() function: Example Print the number of items in the list:
  • 12.
    List Items -Data Types • List items can be of any data type: Example String, int and boolean data types: A list can contain different data types:
  • 13.
    List Items -Data Types • List items can be of any data type: Example String, int and boolean data types: A list can contain different data types:
  • 14.
    List Items -Data Types • type() • From Python's perspective, lists are defined as objects with the data type 'list’: <class 'list'>
  • 15.
    The list() Constructor •It is also possible to use the list() constructor when creating a new list. Example • Using the list() constructor to make a List: