(Python Basic Tutorial VIII) list operation in Python

Posted by smallflower on Wed, 06 May 2020 03:17:12 +0200

  1. Python Basic Tutorial
  2. Configuring the Python environment in SublimeEditor
  3. Add comments to Python code
  4. Use of variables in Python
  5. Data types in Python
  6. Keyword in Python
  7. Python string operations
  8. list operation in Python
  9. Tuple operation in Python
  10. Pythonmax() and min() - Find the maximum and minimum values in a list or array
  11. Python finds the largest N (first N) or smallest N items
  12. Python Read and Write CSV Files
  13. Examples of using httplib2-HTTPGET and POST in Python
  14. Python boxes tuple as a variable or parameter
  15. Python Out-of-Box Tuple - Too many values cannot be uncompressed
  16. Pythonmultidict example - Mapping a single key to multiple values in a dictionary
  17. PythonOrderedDict - Ordered Dictionary
  18. Python Dictionary Intersection - Compare Two Dictionaries
  19. Python Priority Queue Example

stay In Python , the list is:

  • order
  • Index (index starts at 0)
  • Variable
  • Heterogeneous (items in the list do not have to be of the same type)
  • A comma-separated list of values written between square brackets
listOfSubjects = ['physics', 'chemistry', "mathematics"]
listOfIds = [0, 1, 2, 3, 4]
miscList = [0, 'one', 2, 'three']

1. Access list items

To access the values in the list, use the slicing syntax or square brackets in the form of an array index to get a single item or item range.

The index value passed can be positive or negative.If the index is negative, the count starts at the end of the list.

List [m: n] indicates that the sublist begins with index m (including) and ends with index n (excluding).

  • If m is not provided, its value is assumed to be zero.
  • If n is not provided, select the range to the end of the list.
ids = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print( ids[0] )			# 0
print( ids[1:5] )		# [1, 2, 3, 4]
print( ids[ : 3] )		# [0, 1, 2]
print( ids[7 : ] )		# [7, 8, 9]\
print( ids[-8:-5] )		# [2, 3, 4]

2. Modily list

To change a specific item in the list, use its index to reference the item and assign a new value.

charList =  ["a", "b", "c"]
charList [2] = "d"
print (charList)	# ['a', 'b', 'd']

3. Iterate a list

We can use it to iterate through the list item for loop.

charList =  ["a", "b", "c"]

for x in charList:
	print(x)

# a
# b
# c

4. Check if a item exists in the list

Use the'in'keyword to determine if the specified item exists in the list.

charList =  ["a", "b", "c"]
if "a" in charList:
	print("a is present")	# a is present

if "d" in charList:
	print("d is present")
else:
	print("d is NOT present")	# d is NOT present

5. Finding length of the list

Use the len() function to find the length of a given list.

charList =  ["a", "b", "c"]
x = len (charList)
print (x)	# 3

6. Adding items

  • To add an item to the end of the list, use the append(item) method.
  • To add an item at a specific index location, use the insert(index, item) method.If the index is larger than the index length, the item is added to the end of the list.
charList =  ["a", "b", "c"]
charList.append("d")	
charList.append("e")
print (charList)		# ['a', 'b', 'c', 'd', 'e']
charList.insert(5, "f")

print (charList)		# ['a', 'b', 'c', 'd', 'e', 'f']

charList.insert(10, "h")	# No error 

print (charList)	# ['a', 'b', 'c', 'd', 'e', 'f', 'h']

7. Removing items

To delete an item from the list, use one of the four ways: remove(), pop(), clear(), or del keywords.

7.1. remove()

It deletes the specified item from its value.

charList =  ["a", "b", "c"]
charList.remove("c")	
print (charList)		# ['a', 'b']

7.2. pop()

It deletes the specified item through the index.If no index is provided, it will delete the last item from the list.

charList =  ["a", "b", "c", "d"]
charList.pop()			# removes 'd' - last item
print (charList)		# ['a', 'b', 'c']
charList.pop(1)			# removes 'b'
print (charList)		# ['a', 'c']

7.3. clear()

It clears the list.

charList =  ["a", "b", "c", "d"]
charList.clear()	
print (charList)		# []

7.4. del keyword

It can be used to delete items from the index of a list.We can also use it to delete the entire list.

charList =  ["a", "b", "c", "d"]
del charList[0]	

print (charList)		# ['b', 'c', 'd']

del charList

print (charList)		# NameError: name 'charList' is not defined

8. Join two lists

We can add two given lists to Python using the'+'operator or the extend() function.

charList = ["a", "b", "c"]
numList	= [1, 2, 3]

list1 = charList + numList

print (list1)	# ['a', 'b', 'c', 1, 2, 3]

charList.extend(numList)

print (charList)	# ['a', 'b', 'c', 1, 2, 3]

9. Python list methods

9.1. append()

Add an element at the end of the list.

charList =  ["a", "b", "c"]

charList.append("d")

print (charList)	# ["a", "b", "c", "d"]

9.2. clear()

Remove all elements from the list.

charList =  ["a", "b", "c"]

charList.clear()

print (charList)	# []

9.3. copy()

Returns a copy of the list.

charList =  ["a", "b", "c"]

newList = charList.copy()

print (newList)	# ["a", "b", "c"]

9.4. count()

Returns the number of elements with a specified value.

charList =  ["a", "b", "c"]

x = charList.count('a')

print (x)	# 1

9.5. extend()

Add the elements of the list to the end of the current list.

charList = ["a", "b", "c"]
numList	= [1, 2, 3]

charList.extend(numList)

print (charList)	# ['a', 'b', 'c', 1, 2, 3]

9.6. index()

Returns the index of the first element with the specified value.

charList =  ["a", "b", "c"]

x = charList.index('a')

print (x)	# 0

9.7. insert()

Adds an element at a specified location.

charList =  ["a", "b", "c"]

charList.insert(3, 'd')

print (charList)	# ['a', 'b', 'c', 'd']

9.8. pop()

Delete the element at the specified location or end of the list.

charList =  ["a", "b", "c", "d"]

charList.pop()			# removes 'd' - last item

print (charList)		# ['a', 'b', 'c']

charList.pop(1)			# removes 'b'

print (charList)		# ['a', 'c']

9.9. remove()

Deletes an item with a specified value.

charList =  ["a", "b", "c", "d"]

charList.remove('d')

print (charList)		# ['a', 'b', 'c']

9.10. reverse()

Reverse the order of items in the list.

charList =  ["a", "b", "c", "d"]

charList.reverse()

print (charList)		# ['d', 'c', 'b', 'a']

9.11. sort()

By default, the given list is sorted in ascending order.

charList =  ["a", "c", "b", "d"]

charList.sort()

print (charList)		# ["a", "b", "c", "d"]

Happy learning! Author: Distributed programming Source: https://zthinker.com/ If you like this, long press QR code and focus on Distributed Programming .

Topics: Programming Python