ListBasics: Difference between revisions
Migratebot (talk | contribs) (Created page with "
== Making a list ==
<source lang="python"> foo = ["orange", "purple", "blue", "green", "red"] </source>
== Turning a string (text) into a list ==
Strings have a '''...") |
No edit summary |
||
Line 166: | Line 166: | ||
[[Category:Cookbook]] | [[Category:Cookbook]] | ||
[[Category:Python]] |
Latest revision as of 10:33, 6 October 2020
Making a list
foo = ["orange", "purple", "blue", "green", "red"]
Turning a string (text) into a list
Strings have a split function.
text = "Evil media studies deliberately courts the accusation of anachronism so as to both counter and to enhance the often tacit deception and trickery within the precincts of both theory and practice."
words = text.split()
print words
outputs:
['Evil', 'media', 'studies', 'deliberately', 'courts', 'the', 'accusation', 'of', 'anachronism', 'so', 'as', 'to', 'both', 'counter', 'and', 'to', 'enhance', 'the', 'often', 'tacit', 'deception', 'and', 'trickery', 'within', 'the', 'precincts', 'of', 'both', 'theory', 'and', 'practice.']
Turning a list into a string (text)
The quickest way is with the string function join. With join, you start with the text that you want to use to join up the different list elements:
"*".join(words)
output:
'all*work*and*no*play*makes*jack*a*dull*boy'
Here an example using the fact that Python can treat a string variable like a list of letters:
name = "Rachmaninov"
print "".join(reversed(name))
voninamhcaR
Reversing a list
Use the reverse function of a list
Every python list has a function called reverse that changes the list and reverses the order of things.
words = "all work and no play makes jack a dull boy".split()
words.reverse()
print words
outputs:
['boy', 'dull', 'a', 'jack', 'makes', 'play', 'no', 'and', 'work', 'all']
Use the reversed function.
The reversed function allows you to iterate over a list in reverse order, leaving the original list unchanged.
for word in reversed(words):
print word,
# check that the original list is unchanged
print words
output:
boy dull a jack makes play no and work all
['all', 'work', 'and', 'no', 'play', 'makes', 'jack', 'a', 'dull', 'boy']
Use a negative "step" value with list slicing.
A very pythonic way to do this is with a negative "step" value in a list slice, this also leaves the original list unchanged:
words[::-1]
outputs
['boy', 'dull', 'a', 'jack', 'makes', 'play', 'no', 'and', 'work', 'all'
Doing something with every item in a list
Use a for loop:
Filtering a list
Use a for loop + an if statement.
Using list comprehension.
Putting two lists together
Use the "+" operator
The plus sign (+) will add two lists together. Neither list is changed.
words = "all work and no play makes jack a dull boy".split()
nums = range(10)
print words + nums
output:
['all', 'work', 'and', 'no', 'play', 'makes', 'jack', 'a', 'dull', 'boy', 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Using the list extend function
The extend function changes a list by adding a second list onto itself.
words.extend(nums)
print words
output:
['all', 'work', 'and', 'no', 'play', 'makes', 'jack', 'a', 'dull', 'boy', 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Making a (backup) copy of a list
This can be done with slicing as well (very Pythonic ;):
words = "all work and no play makes jack a dull boy".split()
backup = words[:]
words.reverse()
print words
print backup
output:
['boy', 'dull', 'a', 'jack', 'makes', 'play', 'no', 'and', 'work', 'all']
['all', 'work', 'and', 'no', 'play', 'makes', 'jack', 'a', 'dull', 'boy']