Lists in Python
What are lists in Python? Lists are one of the six built-in types of sequences in Python programming language. A sequence is the most basic data structure in Python programming language and each element of a sequence has its own index. The first element has the index 0, the second has the index 1 and so forth. There are operations you can perform with all sequence types and there are specific methods only for each sequence. The len() function helps you to find the length of a sequence like shown in Figure 1.
Inside the len function is a list with three elements. You can see that a list is constructed by square brackets. If you want to find the largest or the smallest element of the list you can use max() (for maximum), and min() for minimum.
Figure 2 shows that we created a list with three elements and assigned it to the a variable. We used the max() and min() function to find the largest and the smallest elements of the list. Now you know that elements in a list are separated by commas and a list can hold any type of element, strings, integers, tuples other lists etc. It does not matter if the items in a list are of the same type or not. Now open your terminal and create a list and assign it to the ‘google’ variable like shown in Figure 2.
Our list is empty and it is time to add some elements to it. We will use the append() method, which is a specific method for lists. I show you how to use the append() method in Figure 4.
To insert an element inside the list with the append() method you put the element you want to append inside the brackets and it is done. You can add as many elements as you want. Time to add more elements with the append() method and find the index for each element on the list. We will use the index() method. This method takes an element of the list as input and finds its index.
We added three new elements on the list, ‘Amibtion’, ‘Larry’ and ‘Steve’. After that we used the index() method to find the index for every element of the list. To find the index of an element with the index() method you put the element you want to find the index as an input for the index() method. Unixmen, how do I extract elements form a list? To extract elements from a list you can use indexing or slicing. Figure 6 teaches you how to use indexing and Figure 7 teaches you how to use slicing.
We use the name of the list and the index of the element we wanted to extract.
Like us on Facebook
We need your assistance to stay live
This week Top Posts
- Top Things To Do After Installing Ubuntu 13.10 'Saucy Salamander' : Ubuntu 13.10 Saucy Salamander will be released on coming October 17th with many new salient featur...0 comments |
- Install Power Commands 0.1.5 On Ubuntu 14.04/13.10/12.10/12.04 : Power Commands, power management application created by the Atareao team , this app helps users to...0 comments |
- Install Uget Download Manager In Ubuntu, Fedora, Debian : uGet is a very light-weight and powerful open source download manager. On March 4th, 2014 Uget Do...0 comments |
- How To Upgrade From Ubuntu 13.04 Raring To Ubuntu 13.10 Saucy Salamander : Ubuntu 13.10 Saucy will be released on October 17th. Hope it will come with lot of improvements and ...0 comments |
- Download Elementary OS 'Luna' Official Wallpapers : Hi Guys, Am a great lover of Elementary OS, so I like to move almost everywhere with some its beaut...0 comments |
- Install Crashplan backup tool in Linux : CashPlan is an automated backup solution that either uses the CrashPlan Central server or any other ...0 comments |
- Fedora 21 Scheduled For Release In October
- Install Uget Download Manager In Ubuntu, Fedora, Debian
- Download Elementary OS ‘Luna’ Official Wallpapers
- Install Power Commands 0.1.5 On Ubuntu 14.04/13.10/12.10/12.04
- 5 Android Apps that’s Going to Make Running Your Business Less Stressful
- Puppet agent Install Using Script
- Install Corosync and Pacemaker On CentOS 6.5
- Install Fail2Ban On Ubuntu Server 13.04/13.10
- How To Install Google Chrome in Fedora 20, 19, 18
- How to surf using Tor Browser Bundle
This work by unixmen.com is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.
Copyright © 2008-2013 Unixmen.com .