Lists in Python Creating lists
Simple ways to create a list Hard code the values mylist = [3, 9, ‘a’, 7.2] Using the replication operator mylist = [0] * 100 (gives a list with 100 zeros) Use the split method on a string Use the readlines method on a file Put [] around a variable if my_num is 5, then [ my_num] is the list [5]
Append vs. Concatenate Both can be used to add to an existing list but their syntax is not the same The concatenate operator + uses two lists and creates a bigger one You cannot say mylist = mylist + “joe”, it must be mylist = mylist + [“joe”] Append is a method which adds an element to the right end of a list – any type of data mylist.append(“3”) makes mylist one element longer
List accumulation Either concatenation or append can be used with lists as accumulators Initialize the list variable as an empty list [] inside your loop either concatenate or append the new item onto your list variable