List Basics in Python and Perl

Xah Lee, 2005-01-11

Python

List Construction

a = [0, 1, 2, 'more',4,5,6]
print a

Counting Elements

a = ['more',4,6]
print len(a) # prints 3

Getting a Element

List element can be extracted by appending a square bracket with index.

a = ['more',4,6]
print a[1] # prints 4

Negative index counts from right.

a = ['more',4,6]
print a[-1] # prints 6

Getting Sublists

Consecutive sequences can be extracted using the form “myList[«startIndex»:«endIndex»]”.

a=['nil','uni','bi','tri','quad','quint','sex']
print a[2:4]   # prints ['bi', 'tri']

WARNING: The extraction is not inclusive. For example, “mylist[2:4]” returns only 2 elements, not 3.

Changing Elements

A element can be changed with the form “mylist[index]=val”.

a=['nilpotent','unisex','bisexual']
a[2]='two'
print a  # prints ['nilpotent', 'unisex', 'two']

A sequence of elements can be changed by assiging to sublist directly. The length of new list need not match the sublist.

a=['nilpotent','unisex','bisexual','tribadism','quadriceps','quintessence','sex']
a[2:4]=["two","three"]
print a

Nested Lists

Lists can be nested arbitrarily. Append extra bracket to get element of nested list.

a = [3,4,[7,8]]
print a
print a[2][1]    # returns 8

Join 2 Lists

Lists can be joined with plus sign.

b = ["a","b"] + [7,6]
print b      # prints ['a', 'b', 7, 6]

Reference: Python Doc↗.

Perl

List Construction

In Perl, a list is created by enclosing elements in the parenthesis (). To assign a list to a variable, the variable must have a @ sign in front. To print a list, load the package Data::Dumper. Example:

@a = (0,1,2,'three',4,5,6,7); # assigns a list to @a.
use Data::Dumper; # loads the list-printing module
print '@a is:', Dumper(\@a);

The backslash in front of @a is necessary. It returns the “reference” of the array @a, and the argument to Dumper must be a reference. Once a list is assigned to a variable, it's called array. Don't worry about these details.

Counting Elements

To find the number of elements in a list, use scalar().

print scalar( @a);

Adding Elements

To add a element, or join two lists, use push().

# perl
use Data::Dumper;
@a = (1,9);
@b = (3,4);
@c = ();
push(@c, @a, @b); # @c is the joined list of @a and @b
print Dumper( \@c);

Getting Elements

To extract list element, append with [index]. The index can be multiple for multiple elements.

@a = (0,1,2,'three',4,5,6,7);
@b = @a[3,1,5];
print Dumper \@b;

Changing Elements

To replace parts, do

@a = (0,1,2,'three',4,5,6,7);
$a[3]= 'newValue';
print ' is', Dumper \@a;

Note the dollar sign above. This tells Perl that this data is a “scalar” as opposed to a “multiple”. In perl, a variable of “scalar” type (such as numbers and strings) starts with a dollar sign. A variable for array (aka list) starts with the sign “@”. A variable for harshes/dictionaries starts with “%”. All perl variables must start with one of “$”, “@”, “%”. (this is a simplified story)

Nested List

To create a nested list: embed a array's reference into the parent list. Example:

@a=(1,2,3);
@b = (4,5, \@a, 7);
print 'nested list is', Dumper \@b;

# or, if you don't have an array, you can do it this way:

@b = (4,5, [1,2,3], 7);
print 'nested list is', Dumper \@b;

To extract element from nested list:

$c = $b[2]->[1];
print '$b[2]->[1] is', $c;

Reference: perldoc perldata↗.


See also:


Page created: 2005-01.
© 2005 by Xah Lee.
Xah Signet