Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python lists tuples sets dictionaries

# list: mutable, can be indexed and sliced, repetitions allowed
l = [0, 1, 2.3, "hello"]
l[0]   >>> 0
l[-1]  >>> "hello"
l[1:2] >>> [1, 2.3]
# sets: as list, but repetions are rejected
s = {0, 2, 63, 5, 5} # the second 5 will be discarded
# tuple: can be indexed, immutable
t = (5, 6, 8, 3)
t[0] >>> 5
t[2] >>> 8
# dictionary: pairs key - value. 
# Types of keys and values must (or, for your sanity, should be) uniform type
# keys are unique
d1 = {"key1" : "value1", "key2" : "value2"}
d2 = {1 : "value1", 2 : "value2"}
d3 = {"key1" : 1, "key2" : 2}
print(d1["key1"]) >>> "value1"
print(d2[1])      >>> "value1"
print(d3["key1"]) >>> 1
Comment

list, set, dict, tuple python

myset = {"apple", "banana", "cherry"}
mylist = ["dog", "cat", "humanoid"]
mydict = {'planet': 'blue', 'edges': 4, 'perimeter':15}
mytuple = ('earth', 'mars', 'mercury')
Comment

python lists tuples sets and dictionaries

# Python3 program for explaining
# use of list, tuple, set and
# dictionary
 
# Lists
l = []
 
# Adding Element into list
l.append(5)
l.append(10)
print("Adding 5 and 10 in list", l)
 
# Popping Elements from list
l.pop()
print("Popped one element from list", l)
print()
 
# Set
s = set()
 
# Adding element into set
s.add(5)
s.add(10)
print("Adding 5 and 10 in set", s)
 
# Removing element from set
s.remove(5)
print("Removing 5 from set", s)
print()
 
# Tuple
t = tuple(l)
 
# Tuples are immutable
print("Tuple", t)
print()
 
# Dictionary
d = {}
 
# Adding the key value pair
d[5] = "Five"
d[10] = "Ten"
print("Dictionary", d)
 
# Removing key-value pair
del d[10]
print("Dictionary", d)
Comment

python lists tuples sets and dictionaries

# Python3 program for explaining
# use of list, tuple, set and
# dictionary
 
# Lists
l = []
 
# Adding Element into list
l.append(5)
l.append(10)
print("Adding 5 and 10 in list", l)
 
# Popping Elements from list
l.pop()
print("Popped one element from list", l)
print()
 
# Set
s = set()
 
# Adding element into set
s.add(5)
s.add(10)
print("Adding 5 and 10 in set", s)
 
# Removing element from set
s.remove(5)
print("Removing 5 from set", s)
print()
 
# Tuple
t = tuple(l)
 
# Tuples are immutable
print("Tuple", t)
print()
 
# Dictionary
d = {}
 
# Adding the key value pair
d[5] = "Five"
d[10] = "Ten"
print("Dictionary", d)
 
# Removing key-value pair
del d[10]
print("Dictionary", d)
Comment

PREVIOUS NEXT
Code Example
Python :: how to let only admins do a command in discord.py 
Python :: find next multiple of 5 python 
Python :: How to calculate distance without numpy 
Python :: mutiple condition in dataframe 
Python :: how to get python list length 
Python :: fibonacci number 
Python :: how to print all items in a list python 
Python :: how to make text to speech in python 
Python :: python re search print 
Python :: gdscript tween 
Python :: python if statement 
Python :: merge a list of dictionaries python 
Python :: for loop get rid of stop words python 
Python :: BURGERS2 solution 
Python :: pygame.events 
Python :: stutter function in python 
Python :: split and only grab first part of string 
Python :: dataframe fill nan with mode 
Python :: pandas remove repeated index 
Python :: Could not find a version that satisfies the requirement ckeditor 
Python :: df.fillna(-999,inplace=True) 
Python :: sha256 python 
Python :: pytorch version python command 
Python :: what does % do in python 
Python :: how to remove some characters from a string in python 
Python :: tk is not defined python 3 
Python :: add list to end of list python 
Python :: python regex to find year 
Python :: python line break inside string 
Python :: how to add subtitle to plot in python 
ADD CONTENT
Topic
Content
Source link
Name
2+7 =