<jupyter_start><jupyter_text>[Lists](https://docs.python.org/3/library/stdtypes.htmllists)<jupyter_code>my_empty_list = []

print('empty list: {}, type: {}'.format(my_empty_list, type(my_empty_list)))
list_of_ints = [1, 2, 6, 7]
list_of_misc = [0.2, 5, 'Python', 'is', 'still fun', '!']

print('lengths: {} and {}'.format(len(list_of_ints), len(list_of_misc)))<jupyter_output><empty_output><jupyter_text>Accessing values<jupyter_code>my_list = ['Python', 'is', 'still', 'cool']

print(my_list[0]) # access elements of the list using an index. List indexes, just like string index, start at 0
print(my_list[3])
## Two dimensional lists

coordinates = [[12.0, 13.3], [0.6, 18.0], [88.0, 1.1]]  # two dimensional

print('first coordinate: {}'.format(coordinates[0])) # this gives you the entire list stored at index 0
print('second element of first coordinate: {}'.format(coordinates[0][1])) # this gives you the specific number at 0,1<jupyter_output>first coordinate: [12.0, 13.3]
second element of first coordinate: 13.3<jupyter_text>Accessing parts of the list - slicing<jupyter_code>my_num_list = [1,2,3,4,5,6,7,8,9]


print(my_num_list[2:5]) # note that the range ends one before the second number
print(my_num_list[:5]) # start at the begining and go till 5
print(my_num_list[5:]) # start at 5 and go all the way to the end
print(my_num_list[:]) # print the whole list<jupyter_output><empty_output><jupyter_text>Updating values<jupyter_code>my_list = [0, 1, 2, 3, 4, 5]
my_list[0] = 99
print(my_list)

# remove first value
del my_list[0]
print(my_list)<jupyter_output><empty_output><jupyter_text>Checking if certain value is present in list<jupyter_code>languages = ['Java', 'C++', 'Go', 'Python', 'JavaScript']

# you can use the in operator just like you did with strings
if 'Python' in languages:
    print('Python is there!')
if 6 not in [1, 2, 3, 7]:
    print('number 6 is not present')<jupyter_output><empty_output><jupyter_text>List are mutable<jupyter_code>original = [1, 2, 3]
modified = original
modified[0] = 99
print('original: {}, modified: {}'.format(original, modified))<jupyter_output><empty_output><jupyter_text>You can get around this by creating new `list`:<jupyter_code>original = [1, 2, 3]
modified = list(original)  # Note list() 
# Alternatively, you can use copy method
# modified = original.copy()
modified[0] = 99
print('original: {}, modified: {}'.format(original, modified))<jupyter_output><empty_output><jupyter_text>`list.append()`<jupyter_code>my_list = [1]
my_list.append('ham')
print(my_list)<jupyter_output><empty_output><jupyter_text>`list.remove()`<jupyter_code>my_list = ['Python', 'is', 'sometimes', 'fun']
my_list.remove('sometimes')
print(my_list)

# If you are not sure that the value is in list, better to check first:
if 'Java' in my_list:
    my_list.remove('Java')
else:
    print('Java is not part of this story.')<jupyter_output><empty_output><jupyter_text>`list.sort()`<jupyter_code>numbers = [8, 1, 6, 5, 10]
numbers.sort()
print('numbers: {}'.format(numbers))

numbers.sort(reverse=True)
print('numbers reversed: {}'.format(numbers))

words = ['this', 'is', 'a', 'list', 'of', 'words']
words.sort()
print('words: {}'.format(words))<jupyter_output><empty_output><jupyter_text>`sorted(list)`While `list.sort()` sorts the list in-place, `sorted(list)` returns a new list and leaves the original untouched:<jupyter_code>numbers = [8, 1, 6, 5, 10]
sorted_numbers = sorted(numbers)
print('numbers: {}, sorted: {}'.format(numbers, sorted_numbers))<jupyter_output><empty_output><jupyter_text>`list.extend()`<jupyter_code>first_list = ['beef', 'ham']
second_list = ['potatoes',1 ,3]
first_list.extend(second_list)
print('first: {}, second: {}'.format(first_list, second_list))<jupyter_output><empty_output><jupyter_text>Alternatively you can also extend lists by summing them:<jupyter_code>first = [1, 2, 3]
second = [4, 5]
first += second  # same as: first = first + second
print('first: {}'.format(first))

# If you need a new list
summed = first + second
print('summed: {}'.format(summed))<jupyter_output><empty_output><jupyter_text>`list.reverse()`<jupyter_code>my_list = ['a', 'b', 'ham']
my_list.reverse()
print(my_list)
dir(my_list) ##list of all functions you can call on a list (pun intended!)<jupyter_output><empty_output><jupyter_text>List Comprehension Every list comprehension in Python includes three elements:new_list = [expression for member in iterable]- Expression is the member itself, a call to a method, or any other valid expression that returns a value. In the example below, the expression x**2 is the square of the member value.- Member is the object or value in the list or iterable. In the example below, the member value is x.- Iterable is a list, set, sequence, generator, or any other object that can return its elements one at a time. In the example below, the iterable is range(10).<jupyter_code># list comprehension example
# find the square of all numbers in a list

sq_lst = [x**2 for x in range(10)]
print(sq_lst)<jupyter_output><empty_output><jupyter_text>using conditionals for list comprehension - A more complete description of the comprehension formula adds support for optional conditionals. The most common way to add conditional logic to a list comprehension is to add a conditional to the end of the expression:new_list = [expression for member in iterable (if conditional)]Here, your conditional statement comes just before the closing bracket.Conditionals are important because they allow list comprehensions to filter out unwanted values, which would normally require a call to filter():<jupyter_code># list comprehension example
# collect all numbers less than 20 that are even 

number_list = [ x for x in range(20) if x % 2 == 0]
print(number_list)
# list comprehension example
# collect all numbers less than 100 that are even and divisble by 5

num_list = [y for y in range(100) if y % 2 == 0 if y % 5 == 0] 
print(num_list)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13390456989247312, 'fraction_numerical': 0.021505376344086023, 'mean_word_length': 6.395031055900621, 'pattern_counts': {'":': 0, '<': 64, '<?xml version=': 0, '>': 64, 'https://': 1, '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': '64922', 'license': 'Apache-2.0', 'n_tokens_mistral': 2109, 'n_tokens_neox': 1960, 'n_words': 630, 'path': 'Basic Python/Basic Concepts/06_Lists_Basic_Concepts.ipynb', 'repo_name': 'Atif0604/PythonForMachineLearning', 'size': '5952'}