More about sequences and Lists – Array concept in Python part 2

Dealing Arrays in programming is very important. Lists are very similar to Array concept in programming language. A list can contain any types of variables. This can include variables like, integer, string etc, whatever you wish to add as a List. We had a session to introduce Array concept in Python. I recommend you to read the first part to get an idea about Arrays/Lists. Sequences and Lists – Array concept in Python.

Python is a good and simple language for SysAdmins automation tasks. Go to the Index page and read more about Python.

In this post, we are going to explain something more about Arrays and its operations. First we can start with element slicing. What’s slicing is, slice out or extract pieces of elements from an array.

  1. Listing all elements from Python Lists
  2. To slice out some portions from a Python Lists
  3. Slicing elements using reverse index
  4. Slicing elements sequentially (at a regular interval)
  5. Appending more elements to a Python List
  6. Searching elements from a Python Lists
  7. Removing elements from Python Lists
  8. Replacing elements in Python Lists
  9. How to check the total length of an Lists?
  10. How to list the large elements in a Python Lists?
  11. How to convert a string to Lists in Python?

Sample Lists to illustrate slicing

students = ['Tom','Ali','Mia','John','Mat','George','P.Nat','Marc','King','Josh']
FYI, the index number starts from 0. So the index value of Tom is 0 and Josh is 9.

1. Listing All Elements In a List

To list all elements in an Lists you can use the following syntax:

listname[:]

The left and right section of the full-colon defines the beginning and ending index value for slicing. Nothing represents start to end. That’s it!

Example

In [8]: students[:]
Out[8]: ['Tom', 'Ali', 'Mia', 'John', 'Mat', 'George', 'P.Nat', 'Marc', 'King', 'Josh']

2. To slice out some portions from a Python Lists

You need to specify the starting and ending Indexes to slice out elements from an array. The important thing is, the last index’s element will not come with the slice. Which means, you need to enter a subsequent index value (your actual element to print + 1).

The examples below will explain this in a better way.

Slicing students name one to five.

In [9]: students[0:5]
Out[9]: ['Tom', 'Ali', 'Mia', 'John', 'Mat']

The actual index value of array element “Mat” is 4, but we entered 0 to 5 to slice out index 0 to 4. That’s it!!

Slicing students name three to eight

In [10]: students[2:9]
Out[10]: ['Mia', 'John', 'Mat', 'George', 'P.Nat', 'Marc', 'King']

Slicing students name three to nine (the last element in that array)

In [11]: students[2:10]
Out[11]: ['Mia', 'John', 'Mat', 'George', 'P.Nat', 'Marc', 'King', 'Josh']

That’s it! Coool..

3. Slicing elements using reverse index

You have to enter the -ve index numbers to slice out elements in backward direction. The negative index starts with -1. That’s -ve index for the last element John is “-1”. Please see the examples pasted below for more clarification.

To print the last element

['Tom', 'Ali', 'Mia', 'John', 'Mat', 'George', 'P.Nat', 'Marc', 'King', 'Josh']
In [13]: students[-1]
Out[13]: 'Josh'

To print student name 5 to 8 (Mat to Marc)

In [19]: students[-6:-2]
Out[19]: ['Mat', 'George', 'P.Nat', 'Marc']

See, the index (-ve) value of Marc is -3 but, as the same way, we need to enter an n+1 value, that is -2 🙂 Got confused? Try it yourself to get more clarification.

To print student name 5 to 9 (upto last name)

As I mentioned, we need a n+1 value, we must enter -1 + 1 = 0 for the last element Josh. That did not work. Please leave the right part of colon empty to slice element upto the last one. Cool!!

In [20]: students[-6:]
Out[20]: ['Mat', 'George', 'P.Nat', 'Marc', 'King', 'Josh']

4. Slicing elements sequentially (at a regular interval)

Here, you need to add one more colon to specify the interval period. Please refer the examples below:

Slice student names from first to last with interval 2. That’s student 1, 3, 5, 7, 9.

In [23]: students[::2]
Out[23]: ['Tom', 'Mia', 'Mat', 'P.Nat', 'King']

Slice student name 4 to 10 with interval 3

In [25]: students[5:11:3]
Out[25]: ['George', 'King']

This can also be done with negative index. You need to specify a negative value after the second colon. That’s it!!

To print elements in reverse order

In [27]: students[::-1]
Out[27]: ['Josh', 'King', 'Marc', 'P.Nat', 'George', 'Mat', 'John', 'Mia', 'Ali', 'Tom']

Slice out student name 10 to 5 with interval 2

In [29]: students[-1:-6:-2]
Out[29]: ['Josh', 'Marc', 'George']

Try it yourself to get clear idea on it!

5. Appending more elements to a Python List

You can simply append elements into an array using + symbol

Example

Current contents in our Lists are;

In [28]: students[:]
Out[28]: ['Tom', 'Ali', 'Mia', 'John', 'Mat', 'George', 'P.Nat', 'Marc', 'King', 'Josh']

To add two more students name

students = students[:] + ['Joseph', 'Jos']

Now check the total number of students

: students[:]
Out[40]:
['Tom','Ali','Mia','John','Mat','George','P.Nat','Marc','King','Josh','Joseph','Jos']

6. Searching elements from a Python List

To search an element you can follow the syntax below:

In [43]: 'John' in students
Out[43]: True

In [44]: 'Job' in students
Out[44]: False

If the searching element existing in the array it will display True otherwise it will be False.

7. Removing elements from Python Lists

You can use the function “del” to delete an element from Python Arrays. See the example pasted below:
Here we need to remove the student John from our students list. The index of John is 3 here.

In [46]: del students[3]

Now check the list, John won’t be there!!

8. Replacing elements in Python Lists

To replace an element in Python Array, please follow up the syntax below:

Here we are going to replace the student name Tom with Job

The index of Tom is 0

In [48]: students[0]='Job'

Cool!! That’s it!

9. How to check the total length of an List?

Which means the total number of elements in an array. You can use the function len to count the total number of elements in a Python Array.

In [50]: len(students)
Out[50]: 11

You got it?!?

10. How to list the large elements in a Python List?

You can use the function “max” to find out the largest element in an array.

In [51]: max(students)
Out[51]: 'P.Nat'

Consider a List with numbers

In [52]: mynum = [1, 2,3,4,5,6,23,166,1999]

In [53]: max(mynum)
Out[53]: 1999

To list smallest element use the Python function “min.”

In [55]: min(mynum)
Out[55]: 1

That’s it!

11. How to convert a string to List in Python?

This can be achieved by using the Python function “list”. Please see the example pasted below:

In [56]: list('My Python Lab')
Out[56]: ['M', 'y', ' ', 'P', 'y', 't', 'h', 'o', 'n', ' ', 'L', 'a', 'b']
In [57]: newarray = list('My Python Lab')

In [59]: newarray[:]
Out[59]: ['M', 'y', ' ', 'P', 'y', 't', 'h', 'o', 'n', ' ', 'L', 'a', 'b']

Thanks for reading this stuff!
Let me know if you have any questions.

Python is a good and simple language for SysAdmins automation tasks. Go to the Index page and read more about Python.

Post navigation

Arunlal A

Senior System Developer at Zeta. Linux lover. Traveller. Let's connect! Whether you're a seasoned DevOps pro or just starting your journey, I'm always eager to engage with like-minded individuals. Follow my blog for regular updates, connect on social media, and let's embark on this DevOps adventure together! Happy coding and deploying!

Leave a Reply

Your email address will not be published. Required fields are marked *