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.
The list has the following characteristics:
#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']
#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]
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.
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
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
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…