Pages

Saturday, July 22, 2017

Python Lists in 5 Minutes or Less .. Part 1

Key Points:

  • A list is a collection of values stored in order -- that is, a list is an ordered group of items or elements

  • Lists can have elements of different types (not necessarily items of the same data type)

  • A list can have duplicate elements

  • It is not necessary to specify the size of the list when declared

  • The list can grow or shrink dynamically during runtime


List Creation

Create an empty list

>>> record = []

Create and initialize a list

>>> record = ['Gary', 25, 'Network Ct, Twin Peaks, WA 90201', 140.32, 'Hardhat Worker']

Initialize a list to a size with an initial value of any data type for each element.

eg.,

Create an array list containing 10 elements each initialized to zero

>>> array = [0]*10

>>> print array
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

Accessing Elements

Access or modify elements by their position. First element is at position (index) zero.

>>> print record
['Gary', 25, 'Network Ct, Twin Peaks, WA 90201', 140.32, 'Hardhat Worker']

>>> print record[4]
Hardhat Worker

>>> record[4] = 'Mailman'

Adding Elements

Add a single item to the end of a list with the help of append() method. The item can be any data type or another list.

>>> record.append('925-93-2176')

Use insert() method to add an item at a certain position in the list. First argument specifies the position.

>>> record.insert(1, 'Doe')

>>> print record
['Gary', 'Doe', 25, 'Network Ct, Twin Peaks, WA 90201', 140.32, 'Mailman', '925-93-2176']

Combining Lists

One way is to add a list to another.

>>> record2 = ['John', 'Keats', 52]
>>> record += record2

>>> print record
['Gary', 'Doe', 25, 'Network Ct, Twin Peaks, WA 90201', 140.32, 'Mailman', '925-93-2176', 'John', 'Keats', 52]

Another way is to use extend() method combine one list with another.

>>> record3 = ['Sears Tower, Chicago, IL 46371', 'Senator']
>>> record.extend(record3)

>>> print record
['Gary', 'Doe', 25, 'Network Ct, Twin Peaks, WA 90201', 140.32, 'Mailman', '925-93-2176', 'John', 'Keats', 52, 'Sears Tower, Chicago, IL 46371', 'Senator']

Removing Elements

pop() method removes and returns the element from a specific position if the position was specified. Otherwise, last element of the list will be removed, and returned to the caller.

>>> record.pop()
'Senator'
>>> record.pop(5)
'Mailman'

del list[idx] is an alternative to calling list.pop(idx).

>>> del record[5]

>>> print record
['Gary', 'Doe', 25, 'Network Ct, Twin Peaks, WA 90201', 140.32, '925-93-2176', 'John', 'Keats', 52, 'Sears Tower, Chicago, IL 46371']

remove() method removes an element by value. In case of duplicates, this method removes only the first occurrence of the element.

>>> record.remove(52)

>>> print record
['Gary', 'Doe', 25, 'Network Ct, Twin Peaks, WA 90201', 140.32, '925-93-2176', 'John', 'Keats', 'Sears Tower, Chicago, IL 46371']

List Size (length)

The built-in len() method returns the length of a list.

>>> len(record)
9

No comments:

Post a Comment