Python3 | Lists, Tuples, Dictionaries

list

a = [apple, banana, orange]

Extraction of each element

Each element can be retrieved by adding [] immediately after the variable name and substituting a numerical value.

a = ['Bulbasaur', 'Charmander', 'Squirtle']

print(a[0]) #Bulbasaur
print(a[1]) #Charmander
print(a[2]) #Squirtle

You can retrieve the list as it is by using:.

a = ['Bulbasaur', 'Charmander', 'Squirtle']

print(a[0:3]) # ['Bulbasaur', 'Charmander', 'Squirtle']

List in list

You can store the list in the list. At this time, what can be taken out with [] is Note that it is only the list in the list.

a = [['Bulbasaur', 'Charmander', 'Squirtle'], ['Chikorita', 'Cyndaquil', 'Totodile']]

print(a[0]) # ['Bulbasaur', 'Charmander', 'Squirtle']
print(a[1]) # ['Chikorita', 'Cyndaquil', 'Totodile']

Tuple

b = {'Treecko', 'Torchic', 'Mudkip'}

What is the difference between a list and a tuple?

Is the big difference between lists and tuples __mutable __? That's what it means.

You might think that it would be more convenient to be able to change it, Tuples are useful for data that you don't want to change.

For example, I don't want you to change __ semi-invariant data such as latitude and longitude of the map. Tuples are used in such cases.

dictionary

c = {'Turtwig': 'Grass', 'Chimchar': 'Fire', 'Piplup': 'Water'}

print(c['Turtwig']) # 'Grass'
print(c) # {'Turtwig': 'Grass', 'Chimchar': 'Fire', 'Piplup': 'Water'}

Swap / add elements

Elements can be replaced by assigning to the key itself.

c['Turtwig'] = 'grass'
print(c['Turtwig']) # 'grass'

Similarly, if you set the key and value as a set, you can add elements.

c = {'Turtwig': 'Grass', 'Chimchar': 'Fire', 'Piplup': 'Water'}

c['Pikachu'] = 'Electric'
print(c) # {'Turtwig': 'Grass', 'Chimchar': 'Fire', 'Piplup': 'Water', 'Pikachu': 'Electric'}

Recommended Posts

Python3 | Lists, Tuples, Dictionaries
Python lists, tuples, dictionaries
Python> Tuples versus Lists
Save lists, dictionaries and tuples to external files python
Understand python lists, dictionaries, and so on.
[Introduction to Python3 Day 7] Chapter 3 Py Tools: Lists, Tuples, Dictionaries, Sets (3.3-3.8)
[Introduction to Python3 Day 5] Chapter 3 Py Tools: Lists, Tuples, Dictionaries, Sets (3.1-3.2.6)
[Introduction to Python3 Day 6] Chapter 3 Py tool lists, tuples, dictionaries, sets (3.2.7-3.2.19)
How to use lists, tuples, dictionaries, and sets
Memorize Python commentary 5 --Lists
[Short sentence] [Python] Format and print lists and dictionaries
How tuples work | Python
Manipulating Python character lists (arrays)
Python Basic Course (5 List Tuples)
Python list and tuples and commas
Paiza Python Primer 5: Basics of Dictionaries
What's new in python3.9 Merge dictionaries
Python
Find memory-consuming lists / arrays on Python
Easily handle lists with python + sqlite3
Make Python dictionaries accessible as attributes
I'm addicted to Python 2D lists
Mixed positional arguments, tuples and dictionaries
[Python] Chapter 04-06 Various data structures (creating dictionaries)
Alternative: Make Python dictionaries accessible as attributes
[Python beginner] Join two lists alternately (5 lines).
Process multiple lists with for in Python