What's the library in Python 3 that uses append

Question:

This is embarrassing but I can’t have this simplified piece of code to work.

big = {}
small = [ 10, 20, 30 ]

print (small)
print (big.append(small))

The given error is:

print (big.append(small))
AttributeError: 'dict' object has no> attribute 'append'

I guess that “append” uses a library but I can’t find that anywhere.
What’s the solution for this error?

Asked By: António Cabral

||

Answers:

.append() is a method on list objects, not on dictionaries.

To add a value to a dictionary, you need to assign it to a key:

big['small'] = small

There is no Python library that lets you use .append() on a dictionary.

If big is meant to be a list as well, then make it a list:

>>> big = []
>>> small = [10, 20, 30]
>>> big.append(small)
>>> big
[[10, 20, 30]]

but note that list.append() alters the list in-place, and returns None; trying to print the return value of big.append(small) will not print the new list.

Also note that list.append() will append the small list as one value, resulting in a nested list. If you wanted to add the elements of small to big, use list.extend():

>>> big = []
>>> small = [10, 20, 30]
>>> big.append(small)
>>> big
[10, 20, 30]
Answered By: Martijn Pieters

The solution is sample

for example

symbols = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’, ‘h’, ‘i’, ‘j’, ‘k’, ‘l’, ‘m’, ‘n’, ‘o’, ‘p’, ‘q’, ‘r’, ‘s’,’t’,’u’, ‘v’, ‘w’, ‘x’, ‘y’, ‘z’, ‘0’, ‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’]

def ID_Generetor():
ID = ""
for i in range(0,15):
data = random.choice(symbols)
⤵️
ID += data
print(ID)

I hope you understand

Answered By: ِAl-Edrisy
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.