<jupyter_start><jupyter_text>Lists<jupyter_code># Creating list
fruits =["apple","mango","orange","banana"]
print("length of list fruits :", len(fruits))

# what if we have differnt types in same list
zee = [1,2,'name', 2, ["asd"]]
print(zee)

#Indezing list
#list indexing is similar to string indexing
print(zee[0])
print(zee[-1])

#----------------------------

print(fruits[:2]) # excluding element at index 2
print(fruits[:])
print(fruits[1:]) # elements from index 1 to last<jupyter_output>1
['asd']
['apple', 'mango']
['apple', 'mango', 'orange', 'banana']
['mango', 'orange', 'banana']<jupyter_text>* The + operator concatenates lists to create a new list. . In this case, that new list is immediately assigned to the existing variable a_list . So this line of code is really a two-step process — concatenation thenassignment — which can (temporarily) consume a lot of memory when you’re dealing with large lists* append() method add just one element, and that one element is added at the end of the list* The extend() method takes one argument, a list, and appends each of the items of the argument to the original list.* The insert() method inserts a single item into a list. The first argument is the index of the first item in thelist that will get bumped out of position<jupyter_code># Adding Elements to list
fruits+['peach'] # concatination with + sign
print(fruits)

fruits = fruits+['peach'] # have to re assign value, otherwise list will not be updated
print(fruits)

fruits.append('grapes')  # append only 'one' element at the last
print(fruits)

fruits.extend(['apricot','watermelon']) # extend method takes list as argument
print(fruits)

fruits.insert(0,'pineapple') # take 2 args
print(fruits)
# Searching for value in list
print(zee.count(2))
print(zee.index(2))# 1st occurance
#print(zee.index("apple"))


# the above line is commented otherwise it will give error. To solve this problem check if elment is present or not
if "apple" in zee:
    print(zee.index('apple'))
else:
    print('element not present in the list')
#Removing items from list

del fruits[1]
print(fruits)

fruits.remove('pineapple')
print(fruits)
# More on removing items from list

name  = fruits.pop()
print("removed element :",name)
print(fruits) # remove the last element from the list

name = fruits.pop(2) # specify the index from which value should be removed
print("removed element :",name)
print(fruits) 
# List are mutalble... Are u guessing where I'm heading 
# we can change the value of list element that is ... list can be changed in-place

fruits[3] = "apple"
print(fruits)<jupyter_output>['mango', 'orange', 'peach', 'apple', 'apricot']<jupyter_text>Empty list are fasle, all other lists are true<jupyter_code># Reversing a string or list
hello = '<NAME>'
print(hello[::-1])
temp = "hello"
print(temp[::-3])
a = [1, 2, 3]
b =a
b.append(4)
print(a)

# to check if both var pointing to same list
print(a is b)

print([] is [])
print([1, 2, 3] is [1, 2, 3])

#If we need a new list with similar content we can use the copy method.
a = [1, 2, 3]
b = a.copy()
print(b is a)
b.append(4)
print(b)
print(a)<jupyter_output>[1, 2, 3, 4]
True
False
False
False
[1, 2, 3, 4]
[1, 2, 3]<jupyter_text>TuplesA tuple is an immutable list. A tuple can not be changed in any way once it is created.Tuples are a lot like lists, but they're immutable so they can't be changed in-placeTuples don't have methods like append, extend and remove because they can't change themselves in-place.Slicing works too, just like a list. When you slice a list, you get a new list; when you slice a tuple, you get anew tuple.<jupyter_code>a_tuple = ("a", "b", "mpilgrim", "z", "example",1,["a"])
print(a_tuple)

a_tuple = ()
print(a_tuple)

#--------------------------

print((1 + 2) * 3) # result is not tuple
(1 + 2,) * 3 # result is tuple

#--------------------------
#It's also possible to create tuples by just separating things with commas and adding no parentheses
a = 1,2,3
print(a)<jupyter_output>('a', 'b', 'mpilgrim', 'z', 'example', 1, ['a'])
()
9

(1, 2, 3)<jupyter_text>Tuples are faster than lists. If you’re defining a constant set of values and all you’re ever going to do with itis iterate through it, use a tuple instead of a list.• It makes your code safer if you “write-protect” data that doesn’t need to be changed. Using a tuple insteadof a list is like having an implied assert statement that shows this data is constant, and that special thought(and a specific function) is required to override that.• Some tuples can be used as dictionary keys, as you’ll see later in this chapter. (Lists can never be used asdictionary keys.)<jupyter_code>#Ignore this for now
print(fruits)
print(fruits[::1])
print(fruits[::])
print(fruits[1:])
print(fruits[1::])<jupyter_output>['mango', 'orange', 'peach', 'apple', 'apricot']
['mango', 'orange', 'peach', 'apple', 'apricot']
['mango', 'orange', 'peach', 'apple', 'apricot']
['orange', 'peach', 'apple', 'apricot']
['orange', 'peach', 'apple', 'apricot']
{'fraction_non_alphanumeric': 0.14179851792509512, 'fraction_numerical': 0.012617664730622872, 'mean_word_length': 4.636568848758465, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '622887', 'license': 'MIT', 'n_tokens_mistral': 1721, 'n_tokens_neox': 1648, 'n_words': 679, 'path': 'Lists and Tuples.ipynb', 'repo_name': 'Ham72/Python', 'size': '4998'}