Most methods can be found in the following link: https://docs.python.org/2/tutorial/datastructures.html
Create a characters list:
lista = ['1', '4', '3', '2', '5', '7', '6', '8', '9']
Convert all characters of the list to int:
lista = map(int, lista)
Show the greatest value from the list:
print max(lista)
Show the lowest value from the list:
print min(lista)
Sum all values from the list
print sum(lista)
Search and returns the index of the value passed by parameter:
lista.index(7)
Sort the list in ascending order:
lista.sort()
Reverse the list
lista.reverse()
Insert all elements of a list at the end of another list:
lista.extend(lista2)
Remove the value passed as parameter (the first found in the list):
lista.remove(14)
Remove the last value from the list:
lista.pop()
Remove the first value from the list:
lista.pop(0)
Count how many times the parameter appear in the list:
lista.count(3)
Convert a string into a list:
lista = []
string = '1 2 3 4 5 6 7 8'
lista = string.split()
Transform a list into a string (separate by comma):
lista = ['1', '4', '3', '2', '5', '7', '6', '8', '9']
string = ', '.join(lista)
print string
Generate a random value from the list:
import random
random.choice(lista)
These are some methods you can use in your algorithm when you are working with lists in Python.
I hope you enjoyed the tips.
No comments:
Post a Comment