Python Lists - coderz.py https://coderzpy.com Keep Coding Keep Cheering! Sun, 27 Nov 2022 12:27:19 +0000 en-US hourly 1 https://wordpress.org/?v=6.9.4 https://coderzpy.com/wp-content/uploads/2022/08/cropped-image1-1-32x32.jpg Python Lists - coderz.py https://coderzpy.com 32 32 Python Lists https://coderzpy.com/python-lists/ https://coderzpy.com/python-lists/#respond Fri, 25 Nov 2022 13:26:59 +0000 http://coderzpy.com/?p=2115 As we have already seen in Data Types in Python that Python Lists are just like dynamically sized arrays, declared in ...

The post Python Lists first appeared on coderz.py.

]]>
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

The post Python Lists first appeared on coderz.py.

]]>
https://coderzpy.com/python-lists/feed/ 0
Iterate over a list in Python https://coderzpy.com/iterate-over-a-list-in-python/ https://coderzpy.com/iterate-over-a-list-in-python/#respond Sat, 26 Nov 2022 13:46:41 +0000 http://coderzpy.com/?p=2117 In Python, there are several different ways to iterate through a list. Let’s examine every method for iterating over a ...

The post Iterate over a list in Python first appeared on coderz.py.

]]>
In Python, there are several different ways to iterate through a list. Let’s examine every method for iterating over a list in Python and compare their performance.

Using For loop:

The “for” loop in Python is a way to run a repetitive block of code traversing over a sequence of any kind. For instance,

# code to iterate over a list
list = [1, 2, 3, 4, 5, 6, 7]
   
# Using for loop
for i in list:
    print(i)

Output:

1
2
3
4
5
6
7
For loop and range():

It is used in case we want to employ the conventional for loop, which loops through numbers x and y. For example,

# code to iterate over a list
list = [1, 2, 3, 4, 5, 6, 7]
   
length = len(list)
   
# Iterating the index
# same as 'for i in range(len(list))'
for i in range(length):
    print(list[i])

Output:

1
2
3
4
5
6
7
Using while loop:

For the while loop to work, one condition must be met. Till that condition is met, the loop iterates continuously. The loops are stopped as soon as it is determined to be false.

# code to iterate over a list
list = [1, 2, 3, 4, 5, 6, 7]
   
length = len(list)
   
i = 0
   
# Iterating using while loop
while i < length:
    print(list[i])
    i += 1

Output:

1
2
3
4
5
6
7
Iterate two Lists simultaneously :

Any quantity of list arguments can be passed to the zip() function. Let’s say there are two lists and you want to serially multiply each element of the first list by each element of the second list, save it to a third (empty) list, and so on.


list1 = [1, 2, 3, 4]
list2  = [5, 6, 7, 8]
reslt = []
for x, y in zip(list1, list2):
	reslt.append(x*y)
print(reslt)	
	

Output:

[5, 12, 21, 32]

Note: also read about Python Lists

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

The post Iterate over a list in Python first appeared on coderz.py.

]]>
https://coderzpy.com/iterate-over-a-list-in-python/feed/ 0
Remove elements from a Python List https://coderzpy.com/remove-elements-from-a-python-list-other-functions/ https://coderzpy.com/remove-elements-from-a-python-list-other-functions/#respond Sun, 27 Nov 2022 12:26:45 +0000 http://coderzpy.com/?p=2125 There are three methods to remove elements from a list: Making use of the remove() method Using the pop() method ...

The post Remove elements from a Python List first appeared on coderz.py.

]]>
There are three methods to remove elements from a list:

  • Making use of the remove() method
  • Using the pop() method of the list object
  • Using the del operator
remove() method:

The list includes a built-in method for Python called removes (). Removing the first element that matches the given criteria from the list is helpful.

Syntax:

list.remove(element)

The element that you want to remove from the list.

ReturnValue

There is no return value for this method.

For instance,

List = ['A', 'E', 'I', 'O','U']
List.remove('I')
print(List)

Output:

['A', 'E', 'O', 'U']
pop() method:
  • Another frequently used method for list objects is pop(). This method returns the item or element that was removed from the list.
  • The item to be removed must be specified in the remove() method, which is how this method differs from remove().
  • However, when using the pop() function, the item’s index is passed in as an argument, which causes the item to be returned to the specified index.
  • An IndexError is raised if the specified item to be removed cannot be found.

Example:

List = ["CAT",11,22,33,"JAVA",22,33,11]
List.pop(1)
print(List)

Output:

['CAT', 22, 33, 'JAVA', 22, 33, 11]
del operator:

With one exception, this operator differs significantly from the pop() method of the List object. The item or element at the specified index location is removed from the list by the del operator, but unlike the pop() method, the item is not returned.

Example:

List = ["CAT",11,22,33,"JAVA",22,33,11]
del List[1]
print(List)

Output:

['CAT', 22, 33, 'JAVA', 22, 33, 11]

Note: also read about Iterate over a list in Python

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

The post Remove elements from a Python List first appeared on coderz.py.

]]>
https://coderzpy.com/remove-elements-from-a-python-list-other-functions/feed/ 0