Python Basic – What is The List in Python

List in Python

There are 6 types of built-in types in Python. They are – Numeric, Sequence, Mapping, Class, Instance, and Exception. The most basic data structure is Sequence. Each of these elements is assigned to a number called index or position. The first index is zero, then 1, and then increases in order.

Python again has 3 types of basic sequence types that are List, Tuple, and range/xrange Object. In this section, we will discuss the list.

We generally put the value in a variable. However, if we need to assign multiple values at once, declaring variables one by one is inconvenient and time-consuming.

So we can use the list method to make this work more easily,

Example:

   P = 3
   Q = 8
   R = 1
   S = 0
   T = 5
   print(f"This is from one by one variable -> {[P,Q,R,S,T]}")
 
   number = [3,8,1,0,5]
   print(f"This is from List -> {number}")

Output:

This is from one by one variable -> [3, 8, 1, 0, 5]
This is from List -> [3, 8, 1, 0, 5]

Each item placed on this list will be called an element of that list. We can access these elements like this,

Example:

   number = [3,8,1,0,5]
   var1 = number[0]
   var2 = number[1]
   var3 = number[2]
   var4 = number[3]
   var5 = number[4]
   print(var1, var2, var3, var4, var5)

Output:

  3 8 1 0 5

Here, the first index started at zero. That’s why we put this number[0] in the var1 variable, and then increase it in order. As number[1],number[2],number[3],number[4].

We can keep the others on a list. Here, one character will be an element on that list.

Example:

   we = ['e','n','a','b','l','e','g','e','e','k']
   print(we)
   print(we[5], we[6])
   print("The length of this list:",len(we))

Output:

['e', 'n', 'a', 'b', 'l', 'e', 'g', 'e', 'e', 'k']
e g
The length of this list: 10

The total number of elements on a list is the length of that list. Here, len(we) is the length of the list.

Empty List

A blank list can be made like the following,

Example:

   first_list = []
   print(first_list)

Output:

[]

Element type

Different types of data or elements can be kept in a list in Python. For example, some numbers, some strings, or even one or more lists can be kept as an element in one list. Although it is usually a good practice to keep the same element on a list.

Example:

   number = 38
   numbers_list = [number, 10, 83]
   list_object = ["Numbers", 0, numbers_list, 4.56]
 
   print(list_object[0])
   print(list_object[1])
   print(list_object[2])
   print(list_object[2][2])
   print(list_object[3])

Output:

Numbers
0
[38, 10, 83]
83
4.56

String as a List

A string type value behaves like a list in Python; that is, each character in the string seems to be an element of an imaginary list.

Example:

   me = "EnableGeek"
   print(me[6])

Output:

G

That is, me = “EnableGeek” And me = [“E”, “n”, “a”, “b”, “l”, “e”, “G”, “e”, “e”, “k”] is virtually the same. And so the value of me[6] is coming ‘G’.

Share The Tutorial With Your Friends
Twiter
Facebook
LinkedIn
Email
WhatsApp
Skype
Reddit

Check Our Ebook for This Online Course

Advanced topics are covered in this ebook with many practical examples.