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

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

2 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

1 year ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

1 year ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

1 year ago