neural-networks.io

neural-networks.io

Lists in Python



 

# Create lists

 
# Create an empty list
>>> myList=[]
>>> myList
[]
 
# Create lists initialized with a given value
>>> myList=[2]*5
>>> myList
[2, 2, 2, 2, 2]
 
# Create and populate lists / arrays
>>> myList=['1', "second", 3.0]
>>> myList
['1', 'second', 3.0]
 
# Create and populate multidimensionnal lists
>>> myList=[[1,2,3],[4,5]]
>>> myList
[[1, 2, 3], [4, 5]]
 
# Create a list with range()
>>> myList=list(range(10))
>>> myList
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
 

# Get elements

 
# Get element by index from the beginning
>>> myList=[10,20,30,40,50]
>>> myList[1]
20
 
# Get element by index from the end
>>> myList=[10,20,30,40,50]
>>> myList[-2]
40
 

# Slice lists

 
# Get sublist
>>> myList=[10,20,30,40,50]
>>> myList[1:4]
[20, 30, 40]
 
# Get sublist from the beginning
>>> myList=[10,20,30,40,50]
>>> myList[:2]
[10, 20]
 
# Get sublist from the end
>>> myList=[10,20,30,40,50]
>>> myList[-3:]
[30, 40, 50]

 

# Copy lists

 
# Copy a list to another
>>> myList=[1,2,3]
>>> myCopy = myList[:]
>>> myCopy
[1, 2, 3]
>>> myList=[1,2,3]
>>> myCopy = myList
>>> myCopy[0]=5
>>> myList
[5, 2, 3]
 
# Copy lists including mutable items
>>> import copy
>>> myList=[[1],[1,2],[1,2,3]]
>>> myCopy = copy.deepcopy(myList)
>>> myCopy
[[1], [1, 2], [1, 2, 3]]
>>> myList=[[1],[1,2],[1,2,3]]
>>> myCopy=myList[:]
>>> myCopy[1][0]=6
>>> myList
[[1], [6, 2], [1, 2, 3]]
 

# Count elements

 
# Count elements in list
>>> myList=[8, 2, 3, 6, 8]
>>> len(myList)
5
 
# Count the occurences of a given item in the list
>>> myList=["a","a","a","b","c","c"]
>>> myList.count('a')
3
 
# Test is a given value is in the list
>>> myList=[10,20,30,40,50]
>>> 25 in myList
False
 

# Find elements

 
# Find the index of the first occurence of a given value
>>> myList=["a","a","a","b","c","c"]
>>> myList.index('c')
4
 

# Iterate over lists

 
# Iterate over a list
>>> myList=[10,20,30,40,50]
>>> for value in myList:
...     print (value)
... 
10
20
30
40
50
 
# Iterate over a list with indexes
>>> myList=[10,20,30,40,50]
>>> for index, value in enumerate(myList):
...     print (index, value)
... 
0 10
1 20
2 30
3 40
4 50
 

# Insert, append, extend and concatanate

 
# Insert a new element in the list at a given index
>>> myList = [10,20,30,50]
>>> myList.insert(3,40)
>>> myList
[10, 20, 30, 40, 50]
 
# Append a new element to the list
>>> myList=[1,2,3]
>>> myList.append(4)
>>> myList
[1, 2, 3, 4]
>>> myList=[1,2,3]
>>> myList.append([4,5])
>>> myList
[1, 2, 3, [4, 5]]
 
# Extend a list with another list
>>> myList=[1,2,3]
>>> myList.extend([4,5])
>>> myList
[1, 2, 3, 4, 5]
 
# Concatanate lists
>>> myList=[1,2,3]
>>> myList=myList+[4,5]
>>> myList
[1, 2, 3, 4, 5]
>>> x = [1, 2, 3]
>>> y = [4, 5, 6]
>>> x + y
[1, 2, 3, 4, 5, 6]
 
# Duplicate a list
myList=[1,2]
>>> myList*3
[1, 2, 1, 2, 1, 2]
 

# Remove element

 
# Remove an element from list by index
>>> myList=['a','b','c']
>>> del myList[1]
>>> myList
['a', 'c']
 
# Remove an element from list by value
>>> myList=['a','b','c']
>>> myList.remove('b')
>>> myList
['a', 'c']
 

# Reverse lists

 
# Reverse a list
>>> myList=['a','b','c','d']
>>> myList.reverse()
>>> myList
['d', 'c', 'b', 'a']
 

# Delete lists

 
# Delete a list
>>> myList=[1,2,3]
>>> del myList
>>> myList
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'myList' is not defined