Difference between revisions of "Python-lists"

From wikipost
Jump to navigationJump to search
Line 118: Line 118:
 
# NOTE#5: use 'pop()' to delete the last element
 
# NOTE#5: use 'pop()' to delete the last element
  
 +
</pre>
  
 +
 +
===EXAMPLE #2===
 +
 +
<pre>
 +
#
 +
#
 +
# python list examples
 +
#
 +
# #2 - nested lists
 +
 +
 +
list2 = ['joe', 186, ['swimming', 'running'], [1983,2001,2018], 5, 2]
 +
 +
 +
print('list2: ' + str(list2) )
 +
 +
for x in list2:
 +
    print(x)
 +
 +
print('')
 +
print('')
 +
 +
 +
 +
print('iterate over the third element (index 2)')
 +
 +
for x in list2[2]:
 +
    print(x)
 +
 +
print('')
 +
print('')
 +
 +
 +
print('show the length of the list')
 +
print('list2: ' + str(list2) )
 +
print('length: ' + str(len(list2)) + '  (counting nested lists as 1)' )
 +
 +
print('')
 +
print('')
 +
 +
 +
 +
 +
print('show which elements are lists:')
 +
for x in list2:
 +
 +
    if isinstance(x, list):
 +
        print(str(x) + ' is a list')
 +
    else:
 +
        print(str(x) + ' is not a list')
 +
 +
print('')
 +
print('')
 +
 +
 +
 +
print('list2: ' + str(list2) )
 +
print('add the element \'walking\' to the list at index 2:')
 +
list2[2].append('walking')
 +
print('list2: ' + str(list2) )
 +
 +
print('')
 +
print('')
 +
 +
 +
 +
 +
print('list2: ' + str(list2) )
 +
print('remove the first element from the list at index 3:')
 +
list2[3].pop(0)
 +
print('list2: ' + str(list2) )
 +
 +
print('')
 +
print('')
 +
</pre>
 +
 +
 +
 +
===EXAMPLE #3===
 +
 +
<pre>
 +
#
 +
#
 +
# python list examples
 +
#
 +
# #3 - bigger nested list
 +
 +
 +
list3 = [[110.3, 111.5,110.1, 110.2],[110.2,112.7,109.4,111.6],[111.6,109.3,110.8,111.5]]
 +
 +
print('list3: ' + str(list3) )
 +
print('')
 +
 +
 +
print('print each nested list:')
 +
for x in list3:
 +
    print(x)
 +
 +
print('')
 +
print('')
 +
 +
 +
 +
print('only show the second and third element of each nested list')
 +
for x in list3:
 +
 +
    print(x[1], x[2])
 +
 +
print('')
 +
print('')
 +
 +
 +
 +
print('print the index position and the first element of each nested list')
 +
for (i,x) in enumerate(list3):
 +
 +
    print(i, x[0])
 +
 +
print('')
 +
print('')
 +
</pre>
 +
 +
 +
 +
===EXAMPLE #4===
 +
 +
<pre>
 +
#
 +
#
 +
# python list examples
 +
#
 +
# #4 - list containing a dictionary
 +
 +
 +
list4 = [{'symbol': 'ETHBTC', 'price': '0.03014900'}, {'symbol': 'LTCBTC', 'price': '0.00413500'}]
 +
print('list4: ' + str(list4) )
 +
print('')
 +
 +
 +
print('print each nested dictionary:')
 +
for x in list4:
 +
    print(x)
 +
 +
print('')
 +
print('')
 +
 +
 +
# NOTE: below is an example of parsing a dictionary, which is not a list.
 +
#      See examples for dictionaries for more details
 +
#
 +
print('iterate through each dictionary')
 +
for x in list4:
 +
 +
    marketname = x['symbol']
 +
    price = x['price']
 +
 +
    print('market name: ' + str(marketname))
 +
    print('price: ' + str(price))
 +
    print('')
 +
 +
print('')
 +
print('')
 
</pre>
 
</pre>

Revision as of 13:38, 23 December 2020

parsing lists in python


Example #1

Some basic list manipulation

#
#
# python list examples
#
# #1 one-dimensional ('flat/unnested') lists


list1 = [1,2,3.14,'red','green'] 


# NOTE#1: strings may be surrounded by apostrophes or quotes or mixed
#         the output will convert quotes to apostrophes
#
#goodlist = [1,2,3.14,'red','green']  # strings with apostrophes
#goodlist = [1,2,3.14,'red',"green"]  # mixed apostrophes and quotes
#goodlist = [1,2,3.14,"red","green"]  # all quotes

# NOTE#2: don't surround lists with quotes or apostrophes!
#
#wronglist = '[1,2,3.14,"red","green"]' # list surrounded by apostrophes
#wronglist = "[1,2,3.14,'red','green']" # list surrounded by quotes

print('list1: ' + str(list1) )

# NOTE#3: concatenate a list to a string by surrounding it with 'str()'
#
# print('list1: ' + list1)      # incorrect
# print('list1: ' , list1)      # ok, but shows parenthesis and quotes from string
# print('list1: ' + str(list1)) # correct
# print(list1)                  # correct (but not concatenated)

for x in list1:
    print(x)

print('')
print('')



print ('show 4th element: ' + str(list1[3]))

print('')

print('modify 4th element from \'red\' to \'blue\'')

list1[3] = 'blue'

print('')

print ('show 4th element: ' + str(list1[3]))

print('')
print('')




print('add new element \'orange\' at the end:')

list1.append('orange')

print('')

print('list1: ' + str(list1))

print('')
print('')


print('insert new element \'17\'at the beginning:')

list1.insert(0,17)

print('')

print('list1: ' + str(list1))

print('')
print('')



print('delete element with name \'green\':')

list1.remove('green')

print('')

print('list1: ' + str(list1))

print('')
print('')


# NOTE#4: if a list contains multiple elements with the same value then 'remove' only removes the first matching element


print('delete the third element:')

list1.pop(2)

print('')

print('list1: ' + str(list1))

print('')
print('')


# NOTE#5: use 'pop()' to delete the last element


EXAMPLE #2

#
#
# python list examples
#
# #2 - nested lists


list2 = ['joe', 186, ['swimming', 'running'], [1983,2001,2018], 5, 2] 


print('list2: ' + str(list2) )

for x in list2:
    print(x)

print('')
print('')



print('iterate over the third element (index 2)')

for x in list2[2]:
    print(x)

print('')
print('')


print('show the length of the list')
print('list2: ' + str(list2) )
print('length: ' + str(len(list2)) + '   (counting nested lists as 1)' )

print('')
print('')




print('show which elements are lists:')
for x in list2:

    if isinstance(x, list):
        print(str(x) + ' is a list')
    else:
        print(str(x) + ' is not a list')

print('')
print('')



print('list2: ' + str(list2) )
print('add the element \'walking\' to the list at index 2:')
list2[2].append('walking')
print('list2: ' + str(list2) )

print('')
print('')




print('list2: ' + str(list2) )
print('remove the first element from the list at index 3:')
list2[3].pop(0)
print('list2: ' + str(list2) )

print('')
print('')


EXAMPLE #3

#
#
# python list examples
#
# #3 - bigger nested list


list3 = [[110.3, 111.5,110.1, 110.2],[110.2,112.7,109.4,111.6],[111.6,109.3,110.8,111.5]]

print('list3: ' + str(list3) )
print('')


print('print each nested list:')
for x in list3:
    print(x)

print('')
print('')



print('only show the second and third element of each nested list')
for x in list3:

    print(x[1], x[2])

print('')
print('')



print('print the index position and the first element of each nested list')
for (i,x) in enumerate(list3):

    print(i, x[0])

print('')
print('')


EXAMPLE #4

#
#
# python list examples
#
# #4 - list containing a dictionary


list4 = [{'symbol': 'ETHBTC', 'price': '0.03014900'}, {'symbol': 'LTCBTC', 'price': '0.00413500'}]
print('list4: ' + str(list4) )
print('')


print('print each nested dictionary:')
for x in list4:
    print(x)

print('')
print('')

 
# NOTE: below is an example of parsing a dictionary, which is not a list.
#       See examples for dictionaries for more details
#
print('iterate through each dictionary')
for x in list4:

    marketname = x['symbol']
    price = x['price']

    print('market name: ' + str(marketname))
    print('price: ' + str(price))
    print('')

print('')
print('')