Python - Data Type Lists

Introduction

Python list object is a general sequence.

Lists are position-ordered collections of arbitrarily typed objects.

Lists have no fixed size and they are mutable.

Lists can be modified in place by assignment to offsets as well as a variety of list method calls.

List Sequence Operations

Because lists are sequences, lists support all the sequence operations.

For instance, given a three-item list:

Demo

L = [123, 'test', 1.23]            # A list of three different-type objects 
print( len(L) )                             # Number of items in the list

Result

We can index, slice, and so on, just as for strings:

Demo

L = [123, 'test', 1.23]            # A list of three different-type objects 
print( L[0] )                               # Indexing by position 
print( L[:-1] )                             # Slicing a list returns a new list 
print( L + [4, 5, 6] )                      # Concat/repeat make new lists too 
print( L * 2 )
print( L )                                 # We're not changing the original list
#  w  ww.j a  v a 2s  .com

Result

Related Topics