<jupyter_start><jupyter_text>ListsEarlier when discussing strings we introduced the concept of a *sequence* in Python. Lists can be thought of the most general version of a *sequence* in Python. Unlike strings, they are mutable, meaning the elements inside a list can be changed!In this section we will learn about:        1.) Creating lists    2.) Indexing and Slicing Lists    3.) Basic List Methods    4.) Nesting Lists    5.) Introduction to List ComprehensionsLists are constructed with brackets [] and commas separating every element in the list.Let's go ahead and see how we can construct lists!<jupyter_code># Assign a list to an variable named my_list
my_list = [1,2,3]<jupyter_output><empty_output><jupyter_text>We just created a list of integers, but lists can actually hold different object types. For example:<jupyter_code>my_list = ['A string',23,100.232,'o']<jupyter_output><empty_output><jupyter_text>Just like strings, the len() function will tell you how many items are in the sequence of the list.<jupyter_code>len(my_list)<jupyter_output><empty_output><jupyter_text>Indexing and SlicingIndexing and slicing work just like in strings. Let's make a new list to remind ourselves of how this works:<jupyter_code>my_list = ['one','two','three',4,5]
# Grab element at index 0
my_list[0]
# Grab index 1 and everything past it
my_list[1:]
# Grab everything UP TO index 3
my_list[:3]<jupyter_output><empty_output><jupyter_text>We can also use + to concatenate lists, just like we did for strings.<jupyter_code>my_list + ['new item']<jupyter_output><empty_output><jupyter_text>Note: This doesn't actually change the original list!<jupyter_code>my_list<jupyter_output><empty_output><jupyter_text>You would have to reassign the list to make the change permanent.<jupyter_code># Reassign
my_list = my_list + ['add new item permanently']
my_list<jupyter_output><empty_output><jupyter_text>We can also use the * for a duplication method similar to strings:<jupyter_code># Make the list double
my_list * 2
# Again doubling not permanent
my_list<jupyter_output><empty_output><jupyter_text>Basic List MethodsIf you are familiar with another programming language, you might start to draw parallels between arrays in another language and lists in Python. Lists in Python however, tend to be more flexible than arrays in other languages for a two good reasons: they have no fixed size (meaning we don't have to specify how big a list will be), and they have no fixed type constraint (like we've seen above).Let's go ahead and explore some more special methods for lists:<jupyter_code># Create a new list
list1 = [1,2,3]<jupyter_output><empty_output><jupyter_text>Use the **append** method to permanently add an item to the end of a list:<jupyter_code># Append
list1.append('append me!')
# Show
list1<jupyter_output><empty_output><jupyter_text>Use **pop** to "pop off" an item from the list. By default pop takes off the last index, but you can also specify which index to pop off. Let's see an example:<jupyter_code># Pop off the 0 indexed item
list1.pop(0)
# Show
list1
# Assign the popped element, remember default popped index is -1
popped_item = list1.pop()
popped_item
# Show remaining list
list1<jupyter_output><empty_output><jupyter_text>It should also be noted that lists indexing will return an error if there is no element at that index. For example:<jupyter_code>list1[100]<jupyter_output><empty_output><jupyter_text>We can use the **sort** method and the **reverse** methods to also effect your lists:<jupyter_code>new_list = ['a','e','x','b','c']
#Show
new_list
# Use reverse to reverse order (this is permanent!)
new_list.reverse()
new_list
# Use sort to sort the list (in this case alphabetical order, but for numbers it will go ascending)
new_list.sort()
new_list<jupyter_output><empty_output><jupyter_text>Nesting ListsA great feature of of Python data structures is that they support *nesting*. This means we can have data structures within data structures. For example: A list inside a list.Let's see how this works!<jupyter_code># Let's make three lists
lst_1=[1,2,3]
lst_2=[4,5,6]
lst_3=[7,8,9]

# Make a list of lists to form a matrix
matrix = [lst_1,lst_2,lst_3]
# Show
matrix<jupyter_output><empty_output><jupyter_text>We can again use indexing to grab elements, but now there are two levels for the index. The items in the matrix object, and then the items inside that list!<jupyter_code># Grab first item in matrix object
matrix[0]
# Grab first item of the first item in the matrix object
matrix[0][0]<jupyter_output><empty_output><jupyter_text>List ComprehensionsPython has an advanced feature called list comprehensions. They allow for quick construction of lists. To fully understand list comprehensions we need to understand for loops. So don't worry if you don't completely understand this section, and feel free to just skip it since we will return to this topic later.But in case you want to know now, here are a few examples!<jupyter_code># Build a list comprehension by deconstructing a for loop within a []
first_col = [row[0] for row in matrix]
first_col<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.0771484375, 'fraction_numerical': 0.0119140625, 'mean_word_length': 5.96734693877551, 'pattern_counts': {'":': 0, '<': 65, '<?xml version=': 0, '>': 65, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '16', 'dir': 'jupyter-structured-clean-dedup', 'id': '34439', 'license': 'MIT', 'n_tokens_mistral': 1578, 'n_tokens_neox': 1525, 'n_words': 653, 'path': '00-Python Object and Data Structure Basics/04-Lists.ipynb', 'repo_name': 'sri0470/Complete_Python-3_BootCamp_Advanced', 'size': '5120'}