Home » Python

Lists in python

Learn: What is List in Python? In this article, we will learn the concept of mutable sequence types or sequence data types in python. We will also discuss about the syntax and creation of the lists in python.
Submitted by Abhishek Jain, on September 30, 2017

List is a sequence data type. It is mutable as its values in the list can be modified. It is ancollection of ordered set of values enclosed in square brackets []. As it is arranged set of values, we can use index in square brackets [] to identify a particular value belonging to it. The set ofvalues that make up a list are called its elements, and they can be of any type.

List is a container that holds a number of elements in a given order. For accessing an element of the list, indexing is used.

Its syntax is:

variable_name [index]

Here, variable name is name of the list.

It will provide the value at "index+1" in the list. Indices of the lists have to be an integer value which can be either positive or negative. Positive value of index means counting forward from beginning of the list and negative value means counting backward from end of the list.

Let’s look at some example of simple list:

>>>List1 = [1, 2, 3, 4] 
# list of 4 integer elements.
>>>List2 = ["Delhi", "Chennai", "Mumbai"] 
# list of 3 string elements.
>>>List3 = [ ] 
# empty list i.e. list with no element
>>>List4 = ["abc", 10, 20] 
# list with different types of elements
>>>List5 = [1, 2, [6, 7, 8], 3] 
# A list containing another list known as nested list

Example:

>>>print List1 # let's get the values of list before change
[1, 2, 3, 4]
>>> List1 [1] = 7
>>>print List1 # modified list
[1, 7, 3, 4]

Here, 2nd element of the list (accessed using index value 1) is given a new value, so instead of 2 it will be 7.

Note: An index can have a negative value, in that case counting happens from the end of the list.

Creating a list

List can be created in many ways:

  1. By enclosing elements in [ ] (as we have done in above examples.)
  2. Using other lists

Example:

List5=List1 [:]
Here List5 is created as a copy of List1.
>>>print List5
List6 = List1 [0:2]
>>>print List6

Will create List6 having first two elements of List1.

List comprehension

Example:

>>>A= [i**2 for i in range (10)] 
>>>printA
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

In mathematical terms, A can be defined as A = {i2for: i in (0.....9)}. So, we can say that list comprehension is short-hand for creating list.

Let’s create a list of even numbers belonging to 'A' list:

>>> A= [i**2 for i in range (10)]
>>>B = [i for i in A if i % 2 = = 0]
>>>print B
[0, 4, 16, 36, 64]

Using built-in object

List = list ( ) will create an empty list

Example:

>>>List = list ( )
>>>print List
[ ] # empty list

Or

List = list (sequence)

Example:

>>>List = list("String")
>>>print List
['S', 't', 'r', 'i', 'n', 'g']





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.