Categories: python

Python Lists

As we have already seen in Data Types in Python that Python Lists are just like dynamically sized arrays, declared in other languages (vector in C++ and ArrayList in Java). Let us take a look at them in a more detailed way.

Characteristics of Lists:

The list has the following characteristics:

  • The lists are ordered.
  • The list’s element can be accessed by index.
  • The mutable type includes lists.
  • The list types are mutable.
  • The number of various elements can be stored in a list.
  • The list’s items are enclosed in square brackets [] and separated by commas (,).
Creating a list in Python:
#Creating a List with
# the use of Numbers
# (Having duplicate values)
List = [1, 2, 4, 4, 3, 3, 3, 6, 5]
print("\nList with the use of Numbers: ")
print(List)
 
# Creating a List with
# mixed type of values
# (Having numbers and strings)
List = [1, 'a', 'Number', 2, 'For', 10, 'Grades']
print("\nList with the use of Mixed Values: ")
print(List)

Output:

List with the use of Numbers: 
[1, 2, 4, 4, 3, 3, 3, 6, 5]

List with the use of Mixed Values: 
[1, 'a', 'Number', 2, 'For', 10, 'Grades']
Deriving from another List:
#Creating a List with
# the use of Numbers
# (Having duplicate values)
List = [1, 2, 4, 4, 3, 3, 3, 6, 5]
print("\nList with the use of Numbers: ")
print(List)
 
# Creating a List with
# derived from above list
List1 = List[1:3]
print("\nDerived List with the use above list: ")
print(List1)
# Creating a List with
# derived from above list only using last 4 items
List2 = List[-4:]
print("\nDerived List with the use above list: ")
print(List2)

Output:

 List with the use of Numbers: 
[1, 2, 4, 4, 3, 3, 3, 6, 5]

Derived List with the use above list: [2, 4]
Derived List with the use above list: 
[3, 3, 6, 5]
Adding Serial Numbers in a List:

For storing 0 to (n-1) numbers in a list use range(n) function.

For instance,

#Creating a List with
# the use of Numbers
List1 = [x for x in range(11)]
List2 = [x*2 for x in range(11)]
print("\nList 0 to 10: ")
print(List1)
print("\nList with 2's table: ")
print(List2)

Output:

List 0 to 10: 
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

List with 2's table: 
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

Here for is the keyword which is responsible for iterating (or repeating) x in range 0 to 11 and finally storing the iterated value’s 2’s multiple i.e. (x*2) in the list.

Appending to a List:

The use of the built-in append() function allows for the addition of elements to the List. The append() method can only add one element at a time to the list; loops must be used to add multiple elements using the append() method. Because tuples are immutable, they can also be added to the list using the append method.

For instance,

# Creating a List
List = []
print("Initial blank List: ")
print(List)
 
# Addition of Elements
# in the List
List.append('a')
List.append(5)
List.append(4)
print("\nList after Addition of Three elements: ")
print(List)
 
# Adding elements to the List
# using Iterator
for i in range(1, 4):
    List.append(i)
print("\nList after Addition of elements from 1-3: ")
print(List)
 

Output:

Initial blank List: 
[]

List after Addition of Three elements: 
['a', 5, 4]

List after Addition of elements from 1-3: 
['a', 5, 4, 1, 2, 3]

Note: also read about String Functions – II

Follow Me

Please follow me to read my latest post on programming and technology if you like my post.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Recent Posts

What is object oriented design patterns

A design pattern is a reusable solution to a commonly occurring problem in software design. They…

4 months ago

Factory Method Design Pattern in OODP

Factory Method is a creational design pattern that deals with the object creation. It separates…

4 months ago

Find Intersection of Two Singly Linked Lists

You are given two singly linked lists that intersect at some node. Your task is…

10 months ago

Minimum Cost to Paint Houses with K Colors

A builder plans to construct N houses in a row, where each house can be…

10 months ago

Longest Absolute Path in File System Representation

Find the length of the longest absolute path to a file within the abstracted file…

10 months ago

Efficient Order Log Storage

You manage an e-commerce website and need to keep track of the last N order…

11 months ago