# Lists

### Declaring basic list

basicList = [10, 20, 30, 40]

• Lists can be of any type
• They are not the same as arrays, arrays are usually of a single type.
• values in a list are called elements sometimes items

### Nesting lists

basicList = ['spam', 2.0, 5, [10, 20]]

print(basicList) # spam


### Traversing list

cheeses = ['Cheddar', 'Edam', 'Gouda']
for cheese in cheeses:
print(cheese)


### Ranging through list with index

cheeses = ['Cheddar', 'Edam', 'Gouda']                                                                                                        │
│
for i in range(len(cheeses)):                                                                                                                 │
print(i, cheeses[i])


### Concatenate two lists

a = [1, 2, 3]
b = [4, 5, 6]
c = a + b
print(c)


### List method - append

t = ['a', 'b', 'c']
t.append('d')
print(t)

• Adds a new element at the end of a list

### List method - extend

t1 = ['a', 'b', 'c']
t2 = ['d', 'e', 'f']

#t2 remains unmodified.

t1.extend(t2)
print(t1)

• Takes a list as an argument and appends all of the elements
• How is this different than concatenate?

### List method - sort

t = ['d', 'c', 'e', 'b', 'a']
t.sort()
print(t)