Lists in python

Lists in python

A list is a data structure in python which stores a sequential collection of elements of any size. Each element or value that is inside of a list is called an item.

  • A list is flexible i.e. does not have a fixed size.
  • A list is mutable i.e. the contents of a list can be changed.
  • A list can contain elements of same or mixed types.

    Creating a list

  • empty list
list = [ ]
  • list with integer elements
list = [ 2, 3, 4, 5]
  • list with string elements
list = [ "red", "blue", "pink"]
  • list with elements of mixed type
list = [ "red", 2, 5]

CODE

image.png

Built-in list functions

  • len() : To find the length of a list.
  • max() : To find the largest element in a list.
  • min() : To find the smallest element in a list.
  • sum() : To find the sum of all elements in a list.
  • random.shuffle() : To randomly shuffle the elements present in a list.

CODE

image.png

List operators

  • Index operator : To access an element in the list from its index. Python also allows the use of negative numbers as indexes to reference positions relative to the end of the list.

    CODE

    image.png

Accessing a list out of bounds is a common programming error that results in a runtime IndexError. To avoid this error, make sure that you do not use an index beyond len(list) – 1.

Often reference to the first element in a list is mistaken with index 1, but it should be 0. This is called the off-by-one error.

  • Slicing operator : The slicing operator returns a slice of the list using the syntax list[start : end]. The slice is a sublist from start index to index end – 1. You can also use a negative index in slicing.

    CODE

    image.png
  • Concatenation operator (+) : To concatenate or join two lists.

    CODE

    image.png
  • Repetition operator (*) : To replicate elements in a list.

    CODE

    image.png
  • in and not in operator : To check for the presence of an element in a list.

    CODE

    image.png
  • Operators for comparing lists : (>, >=, <, <=, ==, and !=)

    CODE

    image.png

    List methods

  • append() : To add an element to the end of the list.
  • count() : To return the number of times an element appears in the list.
  • extend() : To append all the elements of a list to another.
  • index() : To return the index of the first occurrence of an element in the list.
  • insert() : To insert an element at a given index in the list.
  • pop() : To removes the element at the given position in the list and return it.
  • remove() : To remove the first occurrence of an element from the list.
  • reverse() : To reverse the elements in the list.
  • sort() : To sort the elements in the list in ascending order.

    CODE

    image.png

    Splitting a String into a List

    The split method is useful for splitting items in a string into a list. One can use either a space or a non-space delimiter.

    CODE

    image.png

    Copying lists

    A list can be copied to another using the assignment statement (=). However, this statement does not copy the contents of the list referenced by list1 to list2; instead, it merely copies the reference value from list1 to list2. After this statement, list1 and list2 refer to the same list. The list previously referenced by list2 is no longer referenced; it becomes garbage.

    CODE

    image.png

Happy coding !!