Python Lists

  • November 25, 2022
  • python
JOIN Clause

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

Leave a Reply

Your email address will not be published. Required fields are marked *