Send Close Add comments: (status displays here)
Got it!  This site "robinsnyder.com" uses cookies. You consent to this by clicking on "Got it!" or by continuing to use this website.  Note: This appears on each machine/browser from which this site is accessed.
Python list processing
by RS  admin@robinsnyder.com : 1024 x 640


1. Lists
A list as a data structure that consists of a sequence of elements. In Python, each element can be of any type, including a list.

Because we are interested in data science computations (e.g., using numpy, etc.) all lists in these examples will be elements of all integers or all floats.

2. Example list
The example Python list is the following.
[2,3,5,7,11,]

Note that Python (like many modern languages, but not JSON data) allows a comma after the last element of the list.

A number of different ways of processing the last are now presented. Some are more machine efficient and/or programmer efficient than other ways.


3. While loop
Here is the Python code [#1]

Here is the output of the Python code.


4. For with range
In Python, the for loop is not like a for loop in C, Java, etc. The Python for is more like a for each loop in some other languages.

Here is the Python code [#2]

Here is the output of the Python code.


5. For loop
The Python for construct iterates over a list (or range, etc.).

Here is the Python code [#3]

Here is the output of the Python code.




6. For loop with enumerate
The Python for enumerate returns both the position and the element.

Here is the Python code [#4]

Here is the output of the Python code.


7. Arrays
In Python, an array is a structured data type of elements of the same type that has constant time access via an integer index.

In Python, a list is a structured data type of elements of differing types that has non-constant time access via an integer index.


8. For loop with numpy
The Python for enumerate returns both the position and the element.

Here is the Python code [#5]

Here is the output of the Python code.


9. For loop with numpy
The Python for enumerate returns both the position and the element.

Here is the Python code [#6]

Here is the output of the Python code.


10. Numpy data types
Some common numpy data types are the following. It is often best if you explicitly set the type to be used instead of taking a default type which could vary from platform to platform.

There are also complex types (not covered here).


11. Floats
In general, always use the largest/biggest precision available for floating point computations.

In the specific case of NumPy, one would not use float32 unless one had a really good reason. Instead, one would use float64.

12. End of page

by RS  admin@robinsnyder.com : 1024 x 640