Python Lists Tutorial

Python has six built-in types of sequences. The most commonly used ones are Lists and Tuples. Both are very similar but you can do so much more with Lists than with Tuples because Tuples are immutable basically once you create a Tuple you cannot modify them. Tuples come handy in some cases but most of the time you will be using Lists as they can be updated, appended, deleted, sliced, sorted, etc. In this tutorial we will review the most commonly used methods in a Lists.

How to create a List

>>> cars = ['Toyota','Honda','GM','Ford','BMW']
>>> cars
['Toyota', 'Honda', 'GM', 'Ford', 'BMW']

How to create an empty List

>>> cars = []
>>> cars
[]

How to create a List with 10 entries

>>> cars = ['BMW']*10
>>> cars
['BMW', 'BMW', 'BMW', 'BMW', 'BMW', 'BMW', 'BMW', 'BMW', 'BMW', 'BMW']

How to create an empty List with 10 entries

>>> cars = [None]*10
>>> cars
[None, None, None, None, None, None, None, None, None, None]

How to create a List from a String

>>> hello = list('hello')
>>> hello
['h', 'e', 'l', 'l', 'o']

How to create a List from comma separated Strings

>>> myCars = "Toyota,Honda,GM,Ford,BMW"
>>> cars= myCars.split(",")
>>> cars
['Toyota', 'Honda', 'GM', 'Ford', 'BMW']

How to get an element in the List by index

Index start from 0 at the left to right or from -1 at the right to left
>>> cars = ['Toyota','Honda','GM','Ford','BMW']
>>> cars[2]
'GM'
>>> cars[-2]
'Ford'

How to find the size of the List

>>> cars = ['Toyota','Honda','GM','Ford','BMW']
>>> len(cars)
5

How to find the minimum entry in the List

>>> cars = ['Toyota','Honda','GM','Ford','BMW']
>>> min(cars)
'BMW'

How to find the maximum entry in the List

>>> cars = ['Toyota','Honda','GM','Ford','BMW']
>>> max(cars)
'Toyota'

How to update an entry in the List

>>> cars = ['Toyota','Honda','GM','Ford','BMW']
>>> cars[1] = 'Lexus'
>>> cars
['Toyota', 'Lexus', 'GM', 'Ford', 'BMW']

How to delete an entry in the List by Index

>>> cars = ['Toyota','Honda','GM','Ford','BMW']
>>> del cars[2]
>>> cars
['Toyota', 'Honda', 'Ford', 'BMW']

How to delete an entry in the List by Value

The remove method is used to remove the first occurrence of a value
>>> cars = ['Toyota','Honda','GM','Ford','BMW','Honda']
>>> cars.remove('Honda')
>>> cars
['Toyota', 'GM', 'Ford', 'BMW', 'Honda']
>>> cars.remove('Honda')
>>> cars
['Toyota', 'GM', 'Ford', 'BMW']
>>> cars.remove('Honda')

Traceback (most recent call last):
  File "", line 1, in 
    cars.remove('Honda')
ValueError: list.remove(x): x not in list



How to remove an entry from the end of the List

The pop() method returns the element while removing it from the list
>>> cars = ['Toyota','Honda','GM','Ford','BMW','Honda']
>>> cars.pop()
'Honda'
>>> cars
['Toyota', 'Honda', 'GM', 'Ford', 'BMW']
>>> cars.append(cars.pop())
>>> cars
['Toyota', 'Honda', 'GM', 'Ford', 'BMW']

How to insert an entry in the List

>>> cars = ['Toyota','Honda','GM','Ford','BMW','Honda']
>>> cars.insert(3,'Lexus')
>>> cars
['Toyota', 'Honda', 'GM', 'Lexus', 'Ford', 'BMW', 'Honda']

How to insert an entry in the end of the List

>>> cars = ['Toyota','Honda','GM','Ford','BMW']
>>> cars.append('Lexus')
>>> cars
['Toyota', 'Honda', 'GM', 'Ford', 'BMW', 'Lexus']

How to insert more than one entry in the List

>>> cars = ['Toyota','Honda','GM','Ford','BMW','Honda']
>>> myCars = ['Lexus','Audi','Mercedes Benz']
>>> cars.extend(myCars)
>>> cars
['Toyota', 'Honda', 'GM', 'Ford', 'BMW', 'Honda', 'Lexus', 'Audi', 'Mercedes Benz']
>>> myCars
['Lexus', 'Audi', 'Mercedes Benz']

How to get a subset of the List using Slice

>>> cars = ['Toyota','Honda','GM','Ford','BMW']
>>> myCars = cars[2:]
>>> myCars
['GM', 'Ford', 'BMW']
>>> cars
['Toyota', 'Honda', 'GM', 'Ford', 'BMW']
>>> myCars = cars[1:4]
>>> myCars
['Honda', 'GM', 'Ford']
>>> myCars = cars[3:5]
>>> myCars
['Ford', 'BMW']
>>> myCars = cars[-2:]
>>> myCars
['Ford', 'BMW']
>>> myCars = cars[-3:-1]
>>> myCars
['GM', 'Ford']

How to count the occurrences of an element in the List

>>> cars = ['Toyota','Honda','GM','Ford','BMW','Honda']
>>> cars.count('Honda')
2
>>> cars.count('GM')
1

How to find the index of an element in the List

>>> cars = ['Toyota','Honda','GM','Ford','BMW','Honda']
>>> cars.index('Ford')
3
>>> cars[cars.index('Ford')]
'Ford'

How to reverse the entries in the List

>>> cars = ['Toyota','Honda','GM','Ford','BMW']
>>> cars.reverse()
>>> cars
['BMW', 'Ford', 'GM', 'Honda', 'Toyota']

How to sort the entries in the List

>>> cars = ['Toyota','Honda','GM','Ford','BMW']
>>> cars.sort()
>>> cars
['BMW', 'Ford', 'GM', 'Honda', 'Toyota']

How to loop thru the entries in the List

>>> cars = ['Toyota','Honda','GM','Ford','BMW']
>>> for car in cars:
 print(car)

 
Toyota
Honda
GM
Ford
BMW

How to loop thru the just some entries in the List

>>> cars = ['Toyota','Honda','GM','Ford','BMW','Toyota','Honda','GM','Ford']
>>> for x in xrange(2,7):
 print(cars[x])

 
GM
Ford
BMW
Toyota
Honda

How to loop thru the entries in the List with step size of 2

>>> cars = ['Toyota','Honda','GM','Ford','BMW','Toyota','Honda','GM','Ford']
>>> for x in xrange(0,len(cars),2):
 print(cars[x])

 
Toyota
GM
BMW
Honda
Ford

How to loop and remove the entries in the List

>>> cars = ['Toyota','Honda','GM','Ford','BMW','Toyota','Honda','GM','Ford']
>>> while cars:
 car = cars.pop()
 print(car)

 
Ford
GM
Honda
Toyota
BMW
Ford
GM
Honda
Toyota
>>> cars
[]

How to easily convert List entries to a string

>>> cars = ['Toyota','Honda','GM','Ford','BMW','Toyota','Honda','GM','Ford']
>>> myCars = ', '.join(cars)
>>> myCars
'Toyota, Honda, GM, Ford, BMW, Toyota, Honda, GM, Ford'

How to convert a List to Tuple

>>> cars = ['Toyota','Honda','GM','Ford','BMW']
>>> myCars = tuple(cars)
>>> myCars
('Toyota', 'Honda', 'GM', 'Ford', 'BMW')

How to convert a List to a Dictionary

>>> cars = ['Toyota','Honda','GM','Ford','BMW']
>>> myCars = dict(zip(xrange(0,len(cars)), cars))
>>> myCars
{0: 'Toyota', 1: 'Honda', 2: 'GM', 3: 'Ford', 4: 'BMW'}

No comments:

Post a Comment

NO JUNK, Please try to keep this clean and related to the topic at hand.
Comments are for users to ask questions, collaborate or improve on existing.