Python 2.7 – Fundamentals – Collections – Lists

In the previous post, we dealt with strings. A string is a collection of characters and lists are ordered collections of any kind of data objects. This means that a single list can have numbers, strings and other objects. Lists are ordered i.e the sequence in which items are added to the list is maintained. A list is denoted by items enclosed in square brackets, separated by commas. Some examples:

 

 

 

 

 

 

As you can see above, a list can even contain other lists.

Operators

The following operators are available for lists:

  1. [] – index operator.
  2.  + – concatenation
  3. * – repetition
  4. [:]  – slicing
  5.  in – membership
  6. len – length (this is a function not really an operator)

We will look at the above in more detail.

[] – index operator

Lists are collections of data types. So to access individual items in a list we use the index operator. The lowest index value is zero which represents the first item and the highest index value is the length of the list minus one.

 

 

 

 

 

 

Trying to access an index beyond the length of the list gives an error. Indexes can be used to change the value of items in the list.

+ – concatenation

This operator lets you join two lists together.

 

 

 

 

*- repetition

This repeats the list x number of times.

 

 

 

[:] slicing

The index operator lets you extract one item from the list. The slicing operator lets us extract multiple items from a list. The operator takes two numbers [x:y] where x is the starting index and y is the ending index. Note that the ending index is not inclusive eg. if you try print name[1:3] it will extract character at index 1 and then stop at index 2.

The slicing operator has various permutations and combinations as shown below:

  • [x:y] – extract from x and stop before y
  • [x:] – extract from x till the rest of the string
  • [:y] – extract from the first item till y
  • [-x:] – extract x item from the end
  • [:-y] – extract from the first item and stop before y
  • [:] – no x and y . Extract the whole list

 

 

 

 

 

 

in – membership

The in operator returns True if an item is in the list else False

 

 

 

 

 

len – length

The len function returns the length of a list

 

 

 

 

Methods

Apart from operations, lists have methods which are given below:

  • append() – add an item to end of the list
  • insert() – insert an item at a certain position in the list
  • pop() – removes and returns an item from the list
  • sort() – sort the items in the list
  • reverse() – reverse the sequence of the items in the list
  • del() – deletes an item from the list
  • index() – returns the index of the first occurrence of an item in the list
  • count() – returns the count of occurrences of an item in the list
  • remove() – removes the first occurrence of an item

append(item)

Adds an item to the end of the list. This automatically increases the length of the list by 1

insert(i, item)

Inserts an item at the i-th position in the list. If i is more than the length of the list it will be inserted at the end of the list

pop(i)

If i is not given then it returns and removes the last item from the list. If i is mentioned then it returns the item at the i-th position and removes it from the list. If i is more than the count of items in the list then an error is thrown

sort()

Sorts the items in the list in ascending order. For mixed data types, bool types come before numbers and numbers come before strings . The bool types False and True are sorted as 0 and 1 respectively.

reverse()

Simply reverses the order of the items in the list. Not to be confused with sorting.

del list(i)

del is not exactly a method of list but a function which takes a list as an argument. This deletes the item at the i-th position in the list. If the position is beyond the end of the list, it throws an error.

index(item)

Returns the index of the first occurrence of an item in the list. If the item is not found then it throws an error.

count(item)

Counts the number of occurrences of an item in the list. If the item is not found it returns a zero.

remove(item)

Removes all occurrences of an item in the list. If the item is not found it throws an error.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

range function

The range function allows us to quickly create a list with a range of numbers. The format of the range function is range(start, end, step) where start is the number from which to start, end is the number before which to stop (not inclusive). The step parameter is optional; if not specified the numbers are incremented by 1 otherwise the numbers are generated by the step value. Various examples are given below:

 

 

 

 

 

In the next section we will look at tuples .

 

Save

1 Trackback / Pingback

  1. Python 2.7 – Fundamentals – Collections – Strings – Truelogic Blog

Leave a Reply

Your email address will not be published.


*


This site uses Akismet to reduce spam. Learn how your comment data is processed.